+ }
+
+ 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
+ $duration = (time() - strtotime($pid["executed"])) / 60;
+ if ($duration > 180) {
+ logger("Worker process ".$pid["pid"]." took more than 3 hours. It will be killed now.");
+ posix_kill($pid["pid"], SIGTERM);
+
+ // Question: If a process is stale: Should we remove it or should we reschedule it?
+ // By now we rescheduling it. It's maybe not the wisest decision?
+ q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
+ intval($pid["pid"]));
+ } else
+ logger("Worker process ".$pid["pid"]." now runs for ".round($duration)." minutes. That's okay.", LOGGER_DEBUG);
+ }
+}
+
+function poller_too_much_workers($stage) {
+
+ $queues = get_config("system", "worker_queues");
+
+ if ($queues == 0)
+ $queues = 4;
+
+ $active = poller_active_workers();
+
+ // Decrease the number of workers at higher load
+ $load = current_load();
+ if($load) {
+ $maxsysload = intval(get_config('system','maxloadavg'));
+ if($maxsysload < 1)
+ $maxsysload = 50;
+
+ $maxworkers = $queues;
+
+ // Some magical mathemathics to reduce the workers
+ $exponent = 3;
+ $slope = $maxworkers / pow($maxsysload, $exponent);
+ $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
+
+ logger("Current load stage ".$stage.": ".$load." - maximum: ".$maxsysload." - current queues: ".$active." - maximum: ".$queues, LOGGER_DEBUG);
+
+ }
+
+ return($active >= $queues);