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 $maxsysload = intval(get_config('system','maxloadavg'));
32 if(function_exists('sys_getloadavg')) {
33 $load = sys_getloadavg();
34 if(intval($load[0]) > $maxsysload) {
35 logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
40 if(($argc <= 1) OR ($argv[1] != "no_cron")) {
41 // Run the cron job that calls all other jobs
42 proc_run("php","include/cron.php");
44 // Run the cronhooks job separately from cron for being able to use a different timing
45 proc_run("php","include/cronhooks.php");
47 // Cleaning dead processes
48 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
50 if (!posix_kill($pid["pid"], 0))
51 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
55 // Sleep two seconds before checking for running processes to avoid having too many workers
58 // Checking number of workers
59 $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
61 $queues = intval(get_config("system", "worker_queues"));
66 if ($workers[0]["workers"] >= $queues)
71 while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
73 if(function_exists('sys_getloadavg')) {
74 $load = sys_getloadavg();
75 if(intval($load[0]) > $maxsysload) {
76 logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
81 // Quit the poller once every hour
82 if (time() > ($starttime + 3600))
85 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
86 dbesc(datetime_convert()),
90 $argv = json_decode($r[0]["parameter"]);
94 // Check for existance and validity of the include file
97 if (!validate_include($include)) {
98 logger("Include file ".$argv[0]." is not valid!");
99 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
103 require_once($include);
105 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
107 if (function_exists($funcname)) {
108 logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
109 $funcname($argv, $argc);
111 logger("Process ".getmypid().": ".$funcname." - done");
113 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
115 logger("Function ".$funcname." does not exist");
120 if (array_search(__file__,get_included_files())===0){
121 poller_run($_SERVER["argv"],$_SERVER["argc"]);