2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3 $directory = dirname($_SERVER["argv"][0]);
5 if (substr($directory, 0, 1) != "/")
6 $directory = $_SERVER["PWD"]."/".$directory;
8 $directory = realpath($directory."/..");
13 require_once("boot.php");
15 function poller_run(&$argv, &$argc){
23 @include(".htconfig.php");
24 require_once("include/dba.php");
25 $db = new dba($db_host, $db_user, $db_pass, $db_data);
26 unset($db_host, $db_user, $db_pass, $db_data);
29 if(function_exists('sys_getloadavg')) {
30 $maxsysload = intval(get_config('system','maxloadavg'));
34 $load = sys_getloadavg();
35 if(intval($load[0]) > $maxsysload) {
36 logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
41 if(($argc <= 1) OR ($argv[1] != "no_cron")) {
42 // Run the cron job that calls all other jobs
43 proc_run("php","include/cron.php");
45 // Run the cronhooks job separately from cron for being able to use a different timing
46 proc_run("php","include/cronhooks.php");
48 // Cleaning dead processes
49 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
51 if (!posix_kill($pid["pid"], 0))
52 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
56 // Sleep two seconds before checking for running processes to avoid having too many workers
59 // Checking number of workers
60 if (poller_too_much_workers())
65 while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
67 // Quit the poller once every hour
68 if (time() > ($starttime + 3600))
71 // Count active workers and compare them with a maximum value that depends on the load
72 if (poller_too_much_workers())
75 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
76 dbesc(datetime_convert()),
80 $argv = json_decode($r[0]["parameter"]);
84 // Check for existance and validity of the include file
87 if (!validate_include($include)) {
88 logger("Include file ".$argv[0]." is not valid!");
89 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
93 require_once($include);
95 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
97 if (function_exists($funcname)) {
98 logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
99 $funcname($argv, $argc);
101 logger("Process ".getmypid().": ".$funcname." - done");
103 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
105 logger("Function ".$funcname." does not exist");
110 function poller_too_much_workers() {
112 $queues = get_config("system", "worker_queues");
117 $active = poller_active_workers();
119 // Decrease the number of workers at higher load
120 if(function_exists('sys_getloadavg')) {
121 $load = max(sys_getloadavg());
123 $maxsysload = intval(get_config('system','maxloadavg'));
127 $queues = max(0, ceil($queues * (($maxsysload - $load) / $maxsysload)));
129 logger("Current load: ".$load." - maximum: ".$maxsysload." - current queues: ".$active." - maximum: ".$queues, LOGGER_DEBUG);
133 return($active >= $queues);
136 function poller_active_workers() {
137 $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
139 return($workers[0]["workers"]);
142 if (array_search(__file__,get_included_files())===0){
143 poller_run($_SERVER["argv"],$_SERVER["argc"]);