+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
+ if(function_exists('sys_getloadavg')) {
+ $load = max(sys_getloadavg());
+
+ $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);
+}
+
+function poller_active_workers() {
+ $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
+
+ return($workers[0]["workers"]);
+}
+