]> git.mxchange.org Git - friendica.git/commitdiff
Handle deadlocks centrally
authorMichael <heluecht@pirati.ca>
Tue, 13 Jun 2017 05:52:59 +0000 (05:52 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 13 Jun 2017 05:52:59 +0000 (05:52 +0000)
include/dba.php
include/poller.php

index e4846899dc1da62c3ca32ef892def0ae2d9471cb..e1330d4424de7b9498ee0a8a8cb2ef0abeb04a1f 100644 (file)
@@ -625,8 +625,21 @@ class dba {
                }
 
                if (self::$dbo->errorno != 0) {
-                       logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
-                               $a->callstack(8))."\n".self::replace_parameters($sql, $args);
+                       $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
+                       $called_from = array_shift($trace);
+
+                       // We are having an own error logging in the function "p"
+                       if ($called_from['function'] != 'p') {
+                               // We have to preserve the error code, somewhere in the logging it get lost
+                               $error = self::$dbo->error;
+                               $errorno = self::$dbo->errorno;
+
+                               logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
+                                       $a->callstack(8))."\n".self::replace_parameters($sql, $args);
+
+                               self::$dbo->error = $error;
+                               self::$dbo->errorno = $errorno;
+                       }
                }
 
                $a->save_timestamp($stamp1, 'database');
@@ -662,17 +675,35 @@ class dba {
 
                $args = func_get_args();
 
-               $stmt = call_user_func_array('self::p', $args);
+               // In a case of a deadlock we are repeating the query 10 times
+               $timeout = 10;
 
-               if (is_bool($stmt)) {
-                       $retval = $stmt;
-               } elseif (is_object($stmt)) {
-                       $retval = true;
-               } else {
-                       $retval = false;
-               }
+               do {
+                       $stmt = call_user_func_array('self::p', $args);
 
-               self::close($stmt);
+                       if (is_bool($stmt)) {
+                               $retval = $stmt;
+                       } elseif (is_object($stmt)) {
+                               $retval = true;
+                       } else {
+                               $retval = false;
+                       }
+
+                       self::close($stmt);
+
+               } while ((self::$dbo->errorno = 1213) && (--$timeout > 0));
+
+               if (self::$dbo->errorno != 0) {
+                       // We have to preserve the error code, somewhere in the logging it get lost
+                       $error = self::$dbo->error;
+                       $errorno = self::$dbo->errorno;
+
+                       logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
+                               $a->callstack(8))."\n".self::replace_parameters($sql, $args);
+
+                       self::$dbo->error = $error;
+                       self::$dbo->errorno = $errorno;
+               }
 
                $a->save_timestamp($stamp, "database_write");
 
index 9da8dddf75face21e590e3fcc22bfc268c8a1af7..e0b83dd73c48d6f285273871c64e94618396329c 100644 (file)
@@ -194,10 +194,7 @@ function poller_execute($queue) {
 
        if (!validate_include($include)) {
                logger("Include file ".$argv[0]." is not valid!");
-               $timeout = 10;
-               while (!dba::delete('workerqueue', array('id' => $queue["id"])) && (--$timeout > 0)) {
-                       sleep(1);
-               }
+               dba::delete('workerqueue', array('id' => $queue["id"]));
                return true;
        }
 
@@ -207,11 +204,7 @@ function poller_execute($queue) {
 
        if (function_exists($funcname)) {
                poller_exec_function($queue, $funcname, $argv);
-               $timeout = 10;
-               while (!dba::delete('workerqueue', array('id' => $queue["id"])) && (--$timeout > 0)) {
-                       logger('Delete ID '.$queue["id"], LOGGER_DEBUG);
-                       sleep(1);
-               }
+               dba::delete('workerqueue', array('id' => $queue["id"]));
        } else {
                logger("Function ".$funcname." does not exist");
        }