From 1bc76fdb6dea61e9287db7ab2fb2df4771c93804 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 20 Nov 2020 19:50:08 +0000 Subject: [PATCH] Watchdog mode to check if the daemon is running --- src/Core/Worker.php | 63 ++++++++++++++++++++++++++++++++++++++ static/defaults.config.php | 5 +++ 2 files changed, 68 insertions(+) diff --git a/src/Core/Worker.php b/src/Core/Worker.php index 04e4819cb2..3083eacbfe 100644 --- a/src/Core/Worker.php +++ b/src/Core/Worker.php @@ -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 * diff --git a/static/defaults.config.php b/static/defaults.config.php index b0832d88f3..b51b5935ad 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -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,