]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Load depending number of workers
[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         if (poller_too_much_workers())
60                 return;
61
62         $starttime = time();
63
64         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
65
66                 if(function_exists('sys_getloadavg')) {
67                         $load = sys_getloadavg();
68                         if(intval($load[0]) > $maxsysload) {
69                                 logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
70                                 return;
71                         }
72                 }
73
74                 // Quit the poller once every hour
75                 if (time() > ($starttime + 3600))
76                         return;
77
78                 if (poller_too_much_workers())
79                         return;
80
81                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d",
82                         dbesc(datetime_convert()),
83                         intval(getmypid()),
84                         intval($r[0]["id"]));
85
86                 $argv = json_decode($r[0]["parameter"]);
87
88                 $argc = count($argv);
89
90                 // Check for existance and validity of the include file
91                 $include = $argv[0];
92
93                 if (!validate_include($include)) {
94                         logger("Include file ".$argv[0]." is not valid!");
95                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
96                         continue;
97                 }
98
99                 require_once($include);
100
101                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
102
103                 if (function_exists($funcname)) {
104                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
105                         $funcname($argv, $argc);
106
107                         logger("Process ".getmypid().": ".$funcname." - done");
108
109                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
110                 } else
111                         logger("Function ".$funcname." does not exist");
112         }
113
114 }
115
116 function poller_too_much_workers() {
117
118         if(function_exists('sys_getloadavg')) {
119                 $load = sys_getloadavg();
120
121                 // To-Do
122                 if ($load < 1)
123                         $queues = 10;
124                 elseif ($load < 5)
125                         $queues = 4;
126                 elseif ($load < 10)
127                         $queues = 2;
128                 else
129                         $queues = 1;
130
131         } else {
132                 $queues = intval(get_config("system", "worker_queues"));
133
134                 if ($queues == 0)
135                         $queues = 4;
136         }
137
138         if (poller_active_workers() >= $queues)
139                 return;
140 }
141
142 function poller_active_workers() {
143         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
144
145         return($workers[0]["workers"]);
146 }
147
148 if (array_search(__file__,get_included_files())===0){
149   poller_run($_SERVER["argv"],$_SERVER["argc"]);
150   killme();
151 }
152 ?>