]> git.mxchange.org Git - friendica.git/commitdiff
Force a database reconnection in the daemon to prevent lost connections
authorMichael <heluecht@pirati.ca>
Sun, 10 Jun 2018 22:04:09 +0000 (22:04 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 10 Jun 2018 22:04:09 +0000 (22:04 +0000)
bin/daemon.php
include/dba.php

index a92446c65b134c356aaa4a9fd817a02a97490e8d..b8c8d2e342007be21958c171bc92e43e99c81ff3 100755 (executable)
@@ -97,24 +97,28 @@ logger('Starting worker daemon.', LOGGER_DEBUG);
 echo "Starting worker daemon.\n";
 
 // Switch over to daemon mode.
-if ($pid = pcntl_fork())
+if ($pid = pcntl_fork()) {
        return;     // Parent
+}
 
 fclose(STDIN);  // Close all of the standard
 fclose(STDOUT); // file descriptors as we
 fclose(STDERR); // are running as a daemon.
 
+dba::disconnect();
+
 register_shutdown_function('shutdown');
 
-if (posix_setsid() < 0)
+if (posix_setsid() < 0) {
        return;
+}
 
-if ($pid = pcntl_fork())
+if ($pid = pcntl_fork()) {
        return;     // Parent
+}
 
 // We lose the database connection upon forking
 dba::connect($db_host, $db_user, $db_pass, $db_data);
-unset($db_host, $db_user, $db_pass, $db_data);
 
 Config::set('system', 'worker_daemon_mode', true);
 
@@ -139,6 +143,11 @@ while (true) {
        Worker::spawnWorker($do_cron);
 
        if ($do_cron) {
+               // We force a disconnect and reconnect of the database connection.
+               // This is done to ensure that the connection don't get lost over time.
+               dba::disconnect();
+               dba::connect($db_host, $db_user, $db_pass, $db_data);
+
                $last_cron = time();
        }
 
index 94f93ff81381406cde2089cce4a80fda657dde88..6b98f243e400998bbce63b28e66e1001b0df9c59 100644 (file)
@@ -92,6 +92,26 @@ class dba {
                return self::$connected;
        }
 
+       /**
+        * Disconnects the current database connection
+        */
+       public static function disconnect()
+       {
+               if (is_null(self::$db)) {
+                       return;
+               }
+
+               switch (self::$driver) {
+                       case 'pdo':
+                               self::$db = null;
+                               break;
+                       case 'mysqli':
+                               self::$db->close();
+                               self::$db = null;
+                               break;
+               }
+       }
+
        /**
         * Return the database object.
         * @return PDO|mysqli