From 6e03477598763cec48c93ddbaeb1d49997f324f1 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 15 Dec 2015 23:26:58 +0100 Subject: [PATCH 1/2] Sometimes the function "sys_getloadavg" doesn't return an array. This is a workaround. --- boot.php | 12 ++++++++++++ include/cron.php | 9 +++++---- include/cronhooks.php | 9 +++++---- include/delivery.php | 9 +++++---- include/discover_poco.php | 9 +++++---- include/poller.php | 13 ++++++------- index.php | 9 +++++---- 7 files changed, 43 insertions(+), 27 deletions(-) diff --git a/boot.php b/boot.php index 05a334a1f..cc5694900 100644 --- a/boot.php +++ b/boot.php @@ -1948,3 +1948,15 @@ function validate_include(&$file) { return true; } + +function current_load() { + if (!function_exists('sys_getloadavg')) + return false; + + $load_arr = sys_getloadavg(); + + if (!is_array($load_arr)) + return false; + + return max($load_arr); +} diff --git a/include/cron.php b/include/cron.php index 8bf168ed5..18674817d 100644 --- a/include/cron.php +++ b/include/cron.php @@ -44,10 +44,11 @@ function cron_run(&$argv, &$argc){ $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) $maxsysload = 50; - if(function_exists('sys_getloadavg')) { - $load = sys_getloadavg(); - if(intval($load[0]) > $maxsysload) { - logger('system: load ' . $load[0] . ' too high. cron deferred to next scheduled run.'); + + $load = current_load(); + if($load) { + if(intval($load) > $maxsysload) { + logger('system: load ' . $load . ' too high. cron deferred to next scheduled run.'); return; } } diff --git a/include/cronhooks.php b/include/cronhooks.php index d5b4f3bf6..8c70008e4 100644 --- a/include/cronhooks.php +++ b/include/cronhooks.php @@ -27,10 +27,11 @@ function cronhooks_run(&$argv, &$argc){ $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) $maxsysload = 50; - if(function_exists('sys_getloadavg')) { - $load = sys_getloadavg(); - if(intval($load[0]) > $maxsysload) { - logger('system: load ' . $load[0] . ' too high. Cronhooks deferred to next scheduled run.'); + + $load = current_load(); + if($load) { + if(intval($load) > $maxsysload) { + logger('system: load ' . $load . ' too high. Cronhooks deferred to next scheduled run.'); return; } } diff --git a/include/delivery.php b/include/delivery.php index dc02faaba..4d87b8bb7 100644 --- a/include/delivery.php +++ b/include/delivery.php @@ -58,10 +58,11 @@ function delivery_run(&$argv, &$argc){ $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) $maxsysload = 50; - if(function_exists('sys_getloadavg')) { - $load = sys_getloadavg(); - if(intval($load[0]) > $maxsysload) { - logger('system: load ' . $load[0] . ' too high. Delivery deferred to next queue run.'); + + $load = current_load(); + if($load) { + if(intval($load) > $maxsysload) { + logger('system: load ' . $load . ' too high. Delivery deferred to next queue run.'); return; } } diff --git a/include/discover_poco.php b/include/discover_poco.php index a8e7ec64d..6293317d3 100644 --- a/include/discover_poco.php +++ b/include/discover_poco.php @@ -28,10 +28,11 @@ function discover_poco_run(&$argv, &$argc){ $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) $maxsysload = 50; - if(function_exists('sys_getloadavg')) { - $load = sys_getloadavg(); - if(intval($load[0]) > $maxsysload) { - logger('system: load ' . $load[0] . ' too high. discover_poco deferred to next scheduled run.'); + + $load = current_load(); + if($load) { + if(intval($load) > $maxsysload) { + logger('system: load ' . $load . ' too high. discover_poco deferred to next scheduled run.'); return; } } diff --git a/include/poller.php b/include/poller.php index 8c102a66b..fd6f3922a 100644 --- a/include/poller.php +++ b/include/poller.php @@ -26,14 +26,14 @@ function poller_run(&$argv, &$argc){ unset($db_host, $db_user, $db_pass, $db_data); }; - if(function_exists('sys_getloadavg')) { + $load = current_load(); + if($load) { $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.'); + if(intval($load) > $maxsysload) { + logger('system: load ' . $load . ' too high. poller deferred to next scheduled run.'); return; } } @@ -134,9 +134,8 @@ function poller_too_much_workers($stage) { $active = poller_active_workers(); // Decrease the number of workers at higher load - if(function_exists('sys_getloadavg')) { - $load = max(sys_getloadavg()); - + $load = current_load(); + if($load) { $maxsysload = intval(get_config('system','maxloadavg')); if($maxsysload < 1) $maxsysload = 50; diff --git a/index.php b/index.php index 9fe248e8e..89ed05846 100644 --- a/index.php +++ b/index.php @@ -56,10 +56,11 @@ if(!$install) { $maxsysload_frontend = intval(get_config('system','maxloadavg_frontend')); if($maxsysload_frontend < 1) $maxsysload_frontend = 50; - if(function_exists('sys_getloadavg')) { - $load = sys_getloadavg(); - if(intval($load[0]) > $maxsysload_frontend) { - logger('system: load ' . $load[0] . ' too high. Service Temporarily Unavailable.'); + + $load = current_load(); + if($load) { + if($load > $maxsysload_frontend) { + logger('system: load ' . $load . ' too high. Service Temporarily Unavailable.'); header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable'); header('Retry-After: 300'); die("System is currently unavailable. Please try again later"); From 33f354a68ce9a4d3bd63cc9ad26129a0c2befefa Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 16 Dec 2015 00:14:53 +0100 Subject: [PATCH 2/2] Bugfix for the maximum load check in worker. --- include/poller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/poller.php b/include/poller.php index fd6f3922a..b1d6099ad 100644 --- a/include/poller.php +++ b/include/poller.php @@ -72,6 +72,10 @@ function poller_run(&$argv, &$argc){ while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) { + // Count active workers and compare them with a maximum value that depends on the load + if (poller_too_much_workers(3)) + return; + q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'", dbesc(datetime_convert()), intval(getmypid()), @@ -116,10 +120,6 @@ function poller_run(&$argv, &$argc){ // 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(3)) - return; } }