]> git.mxchange.org Git - friendica.git/blobdiff - include/poller.php
Merge pull request #2685 from annando/1607-poller-information
[friendica.git] / include / poller.php
index 558eda25ffbcfd2eefb827c25dc65b32e56c9c71..ec6653b4882b83f3abaf5664793b7918c927b15f 100644 (file)
@@ -10,6 +10,9 @@ if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
        chdir($directory);
 }
 
+use \Friendica\Core\Config;
+use \Friendica\Core\PConfig;
+
 require_once("boot.php");
 
 function poller_run(&$argv, &$argc){
@@ -26,23 +29,17 @@ function poller_run(&$argv, &$argc){
                unset($db_host, $db_user, $db_pass, $db_data);
        };
 
-       if (poller_max_connections_reached())
+       if ($a->max_processes_reached())
                return;
 
-       $load = current_load();
-       if($load) {
-               $maxsysload = intval(get_config('system','maxloadavg'));
-               if($maxsysload < 1)
-                       $maxsysload = 50;
+       if (poller_max_connections_reached())
+               return;
 
-               if(intval($load) > $maxsysload) {
-                       logger('system: load ' . $load . ' too high. poller deferred to next scheduled run.');
-                       return;
-               }
-       }
+       if (App::maxload_reached())
+               return;
 
        // Checking the number of workers
-       if (poller_too_much_workers(1)) {
+       if (poller_too_much_workers()) {
                poller_kill_stale_workers();
                return;
        }
@@ -61,19 +58,25 @@ function poller_run(&$argv, &$argc){
                sleep(4);
 
        // Checking number of workers
-       if (poller_too_much_workers(2))
+       if (poller_too_much_workers())
                return;
 
+       $cooldown = Config::get("system", "worker_cooldown", 0);
+
        $starttime = time();
 
-       while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
+       while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority`, `created` LIMIT 1")) {
+
+               // Constantly check the number of parallel database processes
+               if ($a->max_processes_reached())
+                       return;
 
                // Constantly check the number of available database connections to let the frontend be accessible at any time
                if (poller_max_connections_reached())
                        return;
 
                // Count active workers and compare them with a maximum value that depends on the load
-               if (poller_too_much_workers(3))
+               if (poller_too_much_workers())
                        return;
 
                q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
@@ -105,13 +108,18 @@ function poller_run(&$argv, &$argc){
 
                require_once($include);
 
-               $funcname=str_replace(".php", "", basename($argv[0]))."_run";
+               $funcname = str_replace(".php", "", basename($argv[0]))."_run";
 
                if (function_exists($funcname)) {
-                       logger("Process ".getmypid()." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
+                       logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
                        $funcname($argv, $argc);
 
-                       logger("Process ".getmypid()." - ID ".$r[0]["id"].": ".$funcname." - done");
+                       if ($cooldown > 0) {
+                               logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
+                               sleep($cooldown);
+                       }
+
+                       logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
 
                        q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
                } else
@@ -134,6 +142,11 @@ function poller_max_connections_reached() {
        // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
        $max = get_config("system", "max_connections");
 
+       // Fetch the percentage level where the poller will get active
+       $maxlevel = get_config("system", "max_connections_level");
+       if ($maxlevel == 0)
+               $maxlevel = 75;
+
        if ($max == 0) {
                // the maximum number of possible user connections can be a system variable
                $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
@@ -162,10 +175,10 @@ function poller_max_connections_reached() {
 
                logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
 
-               $level = $used / $max;
+               $level = ($used / $max) * 100;
 
-               if ($level >= (3/4)) {
-                       logger("Maximum level (3/4) of user connections reached: ".$used."/".$max);
+               if ($level >= $maxlevel) {
+                       logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
                        return true;
                }
        }
@@ -190,12 +203,12 @@ function poller_max_connections_reached() {
 
        logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
 
-       $level = $used / $max;
+       $level = $used / $max * 100;
 
-       if ($level < (3/4))
+       if ($level < $maxlevel)
                return false;
 
-       logger("Maximum level (3/4) of system connections reached: ".$used."/".$max);
+       logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
        return true;
 }
 
@@ -206,7 +219,7 @@ function poller_max_connections_reached() {
 function poller_kill_stale_workers() {
        $r = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
 
-       if (!is_filled_array($r)) {
+       if (!dbm::is_result($r)) {
                // No processing here needed
                return;
        }
@@ -231,13 +244,16 @@ function poller_kill_stale_workers() {
                }
 }
 
-function poller_too_much_workers($stage) {
+function poller_too_much_workers() {
+
 
        $queues = get_config("system", "worker_queues");
 
        if ($queues == 0)
                $queues = 4;
 
+       $maxqueues = $queues;
+
        $active = poller_active_workers();
 
        // Decrease the number of workers at higher load
@@ -254,7 +270,10 @@ function poller_too_much_workers($stage) {
                $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);
+               $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00'");
+               $entries = $s[0]["total"];
+
+               logger("Current load: ".$load." - maximum: ".$maxsysload." - current queues: ".$active."/".$entries." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
 
        }