]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Do a load check during execution of the queue.
[friendica.git] / include / poller.php
1 <?php
2 if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) {
3         $directory = dirname($_SERVER["argv"][0]);
4
5         if (substr($directory, 0, 1) != "/")
6                 $directory = $_SERVER["PWD"]."/".$directory;
7
8         $directory = realpath($directory."/..");
9
10         chdir($directory);
11 }
12
13 require_once("boot.php");
14
15 function poller_run(&$argv, &$argc){
16         global $a, $db;
17
18         if(is_null($a)) {
19                 $a = new App;
20         }
21
22         if(is_null($db)) {
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);
27         };
28
29         $maxsysload = intval(get_config('system','maxloadavg'));
30         if($maxsysload < 1)
31                 $maxsysload = 50;
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.');
36                         return;
37                 }
38         }
39
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");
43
44                 // Run the cronhooks job separately from cron for being able to use a different timing
45                 proc_run("php","include/cronhooks.php");
46
47                 // Cleaning dead processes
48                 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
49                 foreach($r AS $pid)
50                         if (!posix_kill($pid["pid"], 0))
51                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
52                                         intval($pid["pid"]));
53
54         } else
55                 // Sleep two seconds before checking for running processes to avoid having too many workers
56                 sleep(4);
57
58         // Checking number of workers
59         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
60
61         $queues = intval(get_config("system", "worker_queues"));
62
63         if ($queues == 0)
64                 $queues = 4;
65
66         if ($workers[0]["workers"] >= $queues)
67                 return;
68
69         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
70
71                 if(function_exists('sys_getloadavg')) {
72                         $load = sys_getloadavg();
73                         if(intval($load[0]) > $maxsysload) {
74                                 logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
75                                 return;
76                         }
77                 }
78
79                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
80                         dbesc(datetime_convert()),
81                         intval(getmypid()),
82                         intval($r[0]["id"]));
83
84                 $argv = json_decode($r[0]["parameter"]);
85
86                 $argc = count($argv);
87
88                 // Check for existance and validity of the include file
89                 $include = $argv[0];
90
91                 if (!validate_include($include)) {
92                         logger("Include file ".$argv[0]." is not valid!");
93                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
94                         continue;
95                 }
96
97                 require_once($include);
98
99                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
100
101                 if (function_exists($funcname)) {
102                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
103                         $funcname($argv, $argc);
104
105                         logger("Process ".getmypid().": ".$funcname." - done");
106
107                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
108                 } else
109                         logger("Function ".$funcname." does not exist");
110         }
111
112 }
113
114 if (array_search(__file__,get_included_files())===0){
115   poller_run($_SERVER["argv"],$_SERVER["argc"]);
116   killme();
117 }
118 ?>