Watchdog mode to check if the daemon is running

This commit is contained in:
Michael 2020-11-20 19:50:08 +00:00
parent cc4bae6382
commit 1bc76fdb6d
2 changed files with 68 additions and 0 deletions

View file

@ -1073,6 +1073,8 @@ class Worker
*/ */
public static function executeIfIdle() public static function executeIfIdle()
{ {
self::checkDaemonState();
if (!DI::config()->get("system", "frontend_worker")) { if (!DI::config()->get("system", "frontend_worker")) {
return; return;
} }
@ -1259,6 +1261,8 @@ class Worker
self::IPCSetJobState(true); self::IPCSetJobState(true);
} }
self::checkDaemonState();
// Should we quit and wait for the worker to be called as a cronjob? // Should we quit and wait for the worker to be called as a cronjob?
if ($dont_fork) { if ($dont_fork) {
return $added; return $added;
@ -1397,6 +1401,65 @@ class Worker
return (bool)$row['jobs']; 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 * Check if the system is inside the defined maintenance window
* *

View file

@ -191,6 +191,11 @@ return [
// A value of 0 disables the deletion process. // A value of 0 disables the deletion process.
'dbclean-expire-limit' => 1000, '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) // diaspora_test (Boolean)
// For development only. Disables the message transfer. // For development only. Disables the message transfer.
'diaspora_test' => false, 'diaspora_test' => false,