From 99b86c9fd9a14e90c0048b623db952d21624d239 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 29 Jun 2017 21:19:31 +0000 Subject: [PATCH] Just found the handbrake ... --- include/dba.php | 10 ++++++-- include/poller.php | 64 ++++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/include/dba.php b/include/dba.php index 1c63fa5054..e39e6f136e 100644 --- a/include/dba.php +++ b/include/dba.php @@ -627,6 +627,12 @@ class dba { self::$dbo->errorno = mysql_errno(self::$dbo->db); } else { self::$dbo->affected_rows = mysql_affected_rows($retval); + + // Due to missing mysql_* support this here wasn't tested at all + // See here: http://php.net/manual/en/function.mysql-num-rows.php + if (self::$dbo->affected_rows <= 0) { + self::$dbo->affected_rows = mysql_num_rows($retval); + } } break; } @@ -1038,7 +1044,7 @@ class dba { $sql = "DELETE FROM `".$command['table']."` WHERE `". implode("` = ? AND `", array_keys($command['param']))."` = ?"; - logger(dba::replace_parameters($sql, $command['param']), LOGGER_DATA); + logger(self::replace_parameters($sql, $command['param']), LOGGER_DATA); if (!self::e($sql, $command['param'])) { if ($do_transaction) { @@ -1068,7 +1074,7 @@ class dba { $sql = "DELETE FROM `".$table."` WHERE `".$field."` IN (". substr(str_repeat("?, ", count($field_values)), 0, -2).");"; - logger(dba::replace_parameters($sql, $field_values), LOGGER_DATA); + logger(self::replace_parameters($sql, $field_values), LOGGER_DATA); if (!self::e($sql, $field_values)) { if ($do_transaction) { diff --git a/include/poller.php b/include/poller.php index 017c4bc5d3..4ad9553882 100644 --- a/include/poller.php +++ b/include/poller.php @@ -18,7 +18,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { require_once("boot.php"); function poller_run($argv, $argc){ - global $a, $db, $poller_up_start; + global $a, $db, $poller_up_start, $poller_db_duration; $poller_up_start = microtime(true); @@ -104,8 +104,9 @@ function poller_run($argv, $argc){ // If possible we will fetch new jobs for this worker if (!$refetched && Lock::set('poller_worker_process', 0)) { - logger('Blubb: a'); + $stamp = (float)microtime(true); $refetched = find_worker_processes(); + $poller_db_duration += (microtime(true) - $stamp); Lock::remove('poller_worker_process'); } } @@ -545,15 +546,16 @@ function poller_too_much_workers() { } dba::close($entries); - $jobs_per_minute = 0; - - $jobs = dba::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE"); - if ($job = dba::fetch($jobs)) { - $jobs_per_minute = number_format($job['jobs'] / 10, 0); + $intervals = array(1, 10, 60); + $jobs_per_minute = array(); + foreach ($intervals AS $interval) { + $jobs = dba::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE"); + if ($job = dba::fetch($jobs)) { + $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0); + } + dba::close($jobs); } - dba::close($jobs); - - $processlist = ' - jpm: '.$jobs_per_minute.' ('.implode(', ', $listitem).')'; + $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')'; } $entries = poller_total_entries(); @@ -664,34 +666,52 @@ function find_worker_processes($mypid = 0) { if (poller_passing_slow($highest_priority)) { // Are there waiting processes with a higher priority than the currently highest? - $result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ? + $result = dba::p("SELECT `id` FROM `workerqueue` WHERE `executed` <= ? AND `priority` < ? AND NOT `done` ORDER BY `priority`, `created` LIMIT ".intval($limit), - datetime_convert(), $mypid, NULL_DATE, $highest_priority); - if ($result) { - $found = (dba::affected_rows() > 0); + NULL_DATE, $highest_priority); + + while ($id = dba::fetch($result)) { + $ids[] = $id["id"]; } + dba::close($result); + + $found = (count($ids) > 0); if (!$found) { // Give slower processes some processing time - $result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ? + $result = dba::p("SELECT `id` FROM `workerqueue` WHERE `executed` <= ? AND `priority` > ? AND NOT `done` ORDER BY `priority`, `created` LIMIT ".intval($limit), - datetime_convert(), $mypid, NULL_DATE, $highest_priority); - if ($result) { - $found = (dba::affected_rows() > 0); + NULL_DATE, $highest_priority); + + while ($id = dba::fetch($result)) { + $ids[] = $id["id"]; } + dba::close($result); + + $found = (count($ids) > 0); } } // If there is no result (or we shouldn't pass lower processes) we check without priority limit if (!$found) { - $result = dba::e("UPDATE `workerqueue` SET `executed` = ?, `pid` = ? WHERE `executed` <= ? AND NOT `done` ORDER BY `priority`, `created` LIMIT ".intval($limit), - datetime_convert(), $mypid, NULL_DATE); - if ($result) { - $found = (dba::affected_rows() > 0); + $result = dba::p("SELECT `id` FROM `workerqueue` WHERE `executed` <= ? AND NOT `done` ORDER BY `priority`, `created` LIMIT ".intval($limit), NULL_DATE); + + while ($id = dba::fetch($result)) { + $ids[] = $id["id"]; } + dba::close($result); + + $found = (count($ids) > 0); } + + if ($found) { + $sql = "UPDATE `workerqueue` SET `executed` = ?, `pid` = ? WHERE `id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`;"; + array_unshift($ids, datetime_convert(), $mypid); + dba::e($sql, $ids); + } + return $found; }