private static $lock_duration = 0;
private static $last_update;
private static $state;
+ private static $daemon_mode = null;
/**
* Processes the tasks that are in the workerqueue table
if (time() > ($starttime + (DI::config()->get('system', 'cron_interval') * 60))) {
Logger::info('Process lifetime reached, respawning.');
self::unclaimProcess();
- self::spawnWorker();
+ if (self::isDaemonMode()) {
+ self::IPCSetJobState(true);
+ } else {
+ self::spawnWorker();
+ }
return;
}
}
// Cleaning up. Possibly not needed, but it doesn't harm anything.
- if (DI::config()->get('system', 'worker_daemon_mode', false)) {
+ if (self::isDaemonMode()) {
self::IPCSetJobState(false);
}
Logger::info("Couldn't select a workerqueue entry, quitting process", ['pid' => getmypid()]);
Logger::warning('Maximum processes reached, quitting.');
return false;
}
-
+
return true;
}
// Are there fewer workers running as possible? Then fork a new one.
if (!DI::config()->get("system", "worker_dont_fork", false) && ($queues > ($active + 1)) && self::entriesExists()) {
Logger::info("There are fewer workers as possible, fork a new worker.", ['active' => $active, 'queues' => $queues]);
- if (DI::config()->get('system', 'worker_daemon_mode', false)) {
+ if (self::isDaemonMode()) {
self::IPCSetJobState(true);
} else {
self::spawnWorker();
}
// if there are too much worker, we don't spawn a new one.
- if (DI::config()->get('system', 'worker_daemon_mode', false) && ($active > $queues)) {
+ if (self::isDaemonMode() && ($active > $queues)) {
self::IPCSetJobState(false);
}
Logger::info('Spawned new worker', ['cron' => $do_cron, 'pid' => $pid]);
return;
}
-
+
// We now are in the new worker
DBA::connect();
Logger::info('Worker spawned', ['cron' => $do_cron, 'pid' => getmypid()]);
*/
public static function spawnWorker($do_cron = false)
{
+ Logger::notice("Spawn", ['do_cron' => $do_cron, 'callstack' => System::callstack(20)]);
// Worker and daemon are started from the command line.
// This means that this is executed by a PHP interpreter without runtime limitations
if (function_exists('pcntl_fork') && in_array(DI::mode()->getExecutor(), [Mode::DAEMON, Mode::WORKER])) {
}
// after spawning we have to remove the flag.
- if (DI::config()->get('system', 'worker_daemon_mode', false)) {
+ if (self::isDaemonMode()) {
self::IPCSetJobState(false);
}
}
}
// Set the IPC flag to ensure an immediate process execution via daemon
- if (DI::config()->get('system', 'worker_daemon_mode', false)) {
+ if (self::isDaemonMode()) {
self::IPCSetJobState(true);
}
}
// Quit on daemon mode
- if (DI::config()->get('system', 'worker_daemon_mode', false)) {
+ if (self::isDaemonMode()) {
return $added;
}
return (bool)$row['jobs'];
}
+ /**
+ * Checks if the worker is running in the daemon mode.
+ *
+ * @return boolean
+ */
+ public static function isDaemonMode()
+ {
+ if (!is_null(self::$daemon_mode)) {
+ return self::$daemon_mode;
+ }
+
+ if (DI::mode()->getExecutor() == Mode::DAEMON) {
+ return true;
+ }
+
+ $daemon_mode = DI::config()->get('system', 'worker_daemon_mode', false, true);
+ if ($daemon_mode) {
+ return $daemon_mode;
+ }
+
+ $pidfile = DI::config()->get('system', 'pidfile');
+ if (empty($pidfile)) {
+ // No pid file, no daemon
+ self::$daemon_mode = false;
+ return false;
+ }
+
+ if (!is_readable($pidfile)) {
+ // No pid file. We assume that the daemon had been intentionally stopped.
+ self::$daemon_mode = false;
+ return false;
+ }
+
+ $pid = intval(file_get_contents($pidfile));
+ $running = posix_kill($pid, 0);
+
+ self::$daemon_mode = $running;
+ return $running;
+ }
+
/**
* Test if the daemon is running. If not, it will be started
*