X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fpoller.php;h=ec6653b4882b83f3abaf5664793b7918c927b15f;hb=18cbe9f6ff275cbb363fd9a0a5014bc8fe3d9f7b;hp=805e0fd5e92b3cb5db5409274fde410b04d35c00;hpb=14f606957fda1b3144e71847e6310be0b53e184f;p=friendica.git diff --git a/include/poller.php b/include/poller.php index 805e0fd5e9..ec6653b488 100644 --- a/include/poller.php +++ b/include/poller.php @@ -10,8 +10,10 @@ if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) { chdir($directory); } +use \Friendica\Core\Config; +use \Friendica\Core\PConfig; + require_once("boot.php"); -require_once("dbm.php"); function poller_run(&$argv, &$argc){ global $a, $db; @@ -27,19 +29,8 @@ function poller_run(&$argv, &$argc){ unset($db_host, $db_user, $db_pass, $db_data); }; - $max_processes = get_config('system', 'max_processes_backend'); - if (intval($max_processes) == 0) - $max_processes = 5; - - $processlist = dbm::processlist(); - if ($processlist["list"] != "") { - logger("Processcheck: Processes: ".$processlist["amount"]." - Processlist: ".$processlist["list"], LOGGER_DEBUG); - - if ($processlist["amount"] > $max_processes) { - logger("Processcheck: Maximum number of processes for backend tasks (".$max_processes.") reached.", LOGGER_DEBUG); - return; - } - } + if ($a->max_processes_reached()) + return; if (poller_max_connections_reached()) return; @@ -48,7 +39,7 @@ function poller_run(&$argv, &$argc){ return; // Checking the number of workers - if (poller_too_much_workers(1)) { + if (poller_too_much_workers()) { poller_kill_stale_workers(); return; } @@ -67,30 +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; - $starttime = time(); + $cooldown = Config::get("system", "worker_cooldown", 0); - while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) { + $starttime = time(); - // Log the type of database processes - $processlist = dbm::processlist(); - if ($processlist["amount"] != "") { - logger("Processcheck: Processes: ".$processlist["amount"]." - Processlist: ".$processlist["list"], LOGGER_DEBUG); + while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority`, `created` LIMIT 1")) { - if ($processlist["amount"] > $max_processes) { - logger("Processcheck: Maximum number of processes for backend tasks (".$max_processes.") reached.", LOGGER_DEBUG); - return; - } - } + // 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'", @@ -122,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 @@ -253,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 @@ -276,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); }