]> git.mxchange.org Git - friendica.git/commitdiff
Watchdog mode to check if the daemon is running
authorMichael <heluecht@pirati.ca>
Fri, 20 Nov 2020 19:50:08 +0000 (19:50 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 20 Nov 2020 19:50:08 +0000 (19:50 +0000)
src/Core/Worker.php
static/defaults.config.php

index 04e4819cb265b926267c58ec31985eb4f5e79874..3083eacbfe8483c7b373924867c1df0400292d38 100644 (file)
@@ -1073,6 +1073,8 @@ class Worker
         */
        public static function executeIfIdle()
        {
+               self::checkDaemonState();
+
                if (!DI::config()->get("system", "frontend_worker")) {
                        return;
                }
@@ -1259,6 +1261,8 @@ class Worker
                        self::IPCSetJobState(true);
                }
 
+               self::checkDaemonState();
+
                // Should we quit and wait for the worker to be called as a cronjob?
                if ($dont_fork) {
                        return $added;
@@ -1397,6 +1401,65 @@ class Worker
                return (bool)$row['jobs'];
        }
 
+       /**
+        * Test if the daemon is running. If not, it will be started
+        *
+        * @return void
+        */
+       private static function checkDaemonState()
+       {
+               if (!DI::config()->get('system', 'daemon_watchdog', false)) {
+                       return;
+               }
+
+               if (!DI::mode()->isNormal()) {
+                       return;
+               }
+
+               // Check every minute if the daemon is running
+               if (DI::config()->get('system', 'last_daemon_check', 0) + 60 > time()) {
+                       return;
+               }
+
+               DI::config()->set('system', 'last_daemon_check', time());
+
+               $pidfile = DI::config()->get('system', 'pidfile');
+               if (empty($pidfile)) {
+                       // No pid file, no daemon
+                       return;
+               }
+
+               if (!is_readable($pidfile)) {
+                       // No pid file. We assume that the daemon had been intentionally stopped.
+                       return;
+               }
+
+               $pid = intval(file_get_contents($pidfile));
+               if (posix_kill($pid, 0)) {
+                       Logger::info('Daemon process is running', ['pid' => $pid]);
+                       return;
+               }
+
+               Logger::warning('Daemon process is not running', ['pid' => $pid]);
+
+               self::spawnDaemon();
+       }
+
+       /**
+        * Spawn a new daemon process
+        *
+        * @return void
+        */
+       private static function spawnDaemon()
+       {
+               Logger::info('Starting new daemon process');
+               $command = 'bin/daemon.php';
+               $a = DI::app();
+               $process = new Core\Process(DI::logger(), DI::mode(), DI::config(), DI::modelProcess(), $a->getBasePath(), getmypid());
+               $process->run($command, ['start']);
+               Logger::info('New daemon process started');
+       }
+
        /**
         * Check if the system is inside the defined maintenance window
         *
index b0832d88f35f4bfc6358a5534286744e2e17ef11..b51b5935ad0b8f40314ffae75b0665c00961f760 100644 (file)
@@ -191,6 +191,11 @@ return [
                // A value of 0 disables the deletion process.
                'dbclean-expire-limit' => 1000,
 
+               // daemon_watchdog (Boolean)
+               // Enable regular checking if the daemon is running.
+               // If it is not running and hadn't been terminated normally, it will be started automatically.
+               'daemon_watchdog' => false,
+
                // diaspora_test (Boolean)
                // For development only. Disables the message transfer.
                'diaspora_test' => false,