]> git.mxchange.org Git - friendica.git/blobdiff - include/poller.php
Load depending number of worker queues.
[friendica.git] / include / poller.php
index b03dc84af7cb7158d36c0e06c56ac55ac09c792c..fc592d2066c35a72e382bf21e05cb842d31e0c4c 100644 (file)
@@ -26,6 +26,18 @@ function poller_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
+       if(function_exists('sys_getloadavg')) {
+               $maxsysload = intval(get_config('system','maxloadavg'));
+               if($maxsysload < 1)
+                       $maxsysload = 50;
+
+               $load = sys_getloadavg();
+               if(intval($load[0]) > $maxsysload) {
+                       logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
+                       return;
+               }
+       }
+
        if(($argc <= 1) OR ($argv[1] != "no_cron")) {
                // Run the cron job that calls all other jobs
                proc_run("php","include/cron.php");
@@ -45,17 +57,21 @@ function poller_run(&$argv, &$argc){
                sleep(4);
 
        // Checking number of workers
-       $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
+       if (poller_too_much_workers())
+               return;
 
-       $queues = intval(get_config("system", "worker_queues"));
+       $starttime = time();
 
-       if ($queues == 0)
-               $queues = 4;
+       while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
 
-       if ($workers[0]["workers"] >= $queues)
-               return;
+               // Quit the poller once every hour
+               if (time() > ($starttime + 3600))
+                       return;
+
+               // Count active workers and compare them with a maximum value that depends on the load
+               if (poller_too_much_workers())
+                       return;
 
-       while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
                q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
                        dbesc(datetime_convert()),
                        intval(getmypid()),
@@ -91,6 +107,38 @@ function poller_run(&$argv, &$argc){
 
 }
 
+function poller_too_much_workers() {
+
+       $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;
+
+               $queues = max(0, ceil($queues * (($maxsysload - $load) / $maxsysload)));
+
+               logger("Current load: ".$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"]);
+}
+
 if (array_search(__file__,get_included_files())===0){
   poller_run($_SERVER["argv"],$_SERVER["argc"]);
   killme();