+ foreach($r AS $pid)
+ if (!posix_kill($pid["pid"], 0))
+ q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
+ intval($pid["pid"]));
+ else {
+ // Kill long running processes
+
+ // Check if the priority is in a valid range
+ if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE)))
+ $pid["priority"] = PRIORITY_MEDIUM;
+
+ // Define the maximum durations
+ $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
+ $max_duration = $max_duration_defaults[$pid["priority"]];
+
+ $argv = json_decode($pid["parameter"]);
+ $argv[0] = basename($argv[0]);
+
+ // How long is the process already running?
+ $duration = (time() - strtotime($pid["executed"])) / 60;
+ if ($duration > $max_duration) {
+ logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
+ posix_kill($pid["pid"], SIGTERM);
+
+ // We killed the stale process.
+ // To avoid a blocking situation we reschedule the process at the beginning of the queue.
+ // Additionally we are lowering the priority.
+ q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `created` = '%s',
+ `priority` = %d, `pid` = 0 WHERE `pid` = %d",
+ dbesc(datetime_convert()),
+ intval(PRIORITY_NEGLIGIBLE),
+ intval($pid["pid"]));
+ } else
+ logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
+ }
+}