]> git.mxchange.org Git - friendica.git/blob - include/poller.php
06a5314a4f3b6d6e29dbdd375610c8b705e61a3b
[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         if(function_exists('sys_getloadavg')) {
30                 $maxsysload = intval(get_config('system','maxloadavg'));
31                 if($maxsysload < 1)
32                         $maxsysload = 50;
33
34                 $load = sys_getloadavg();
35                 if(intval($load[0]) > $maxsysload) {
36                         logger('system: load ' . $load[0] . ' too high. poller deferred to next scheduled run.');
37                         return;
38                 }
39         }
40
41         // Checking the number of workers
42         if (poller_too_much_workers(1))
43                 return;
44
45         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
46                 // Run the cron job that calls all other jobs
47                 proc_run("php","include/cron.php");
48
49                 // Run the cronhooks job separately from cron for being able to use a different timing
50                 proc_run("php","include/cronhooks.php");
51
52                 // Cleaning dead processes
53                 $r = q("SELECT DISTINCT(`pid`) FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
54                 foreach($r AS $pid)
55                         if (!posix_kill($pid["pid"], 0))
56                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
57                                         intval($pid["pid"]));
58                         else {
59                                 // To-Do: Kill long running processes
60                                 // But: Update processes (like the database update) mustn't be killed
61                         }
62
63         } else
64                 // Sleep four seconds before checking for running processes again to avoid having too many workers
65                 sleep(4);
66
67         // Checking number of workers
68         if (poller_too_much_workers(2))
69                 return;
70
71         $starttime = time();
72
73         while ($r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `created` LIMIT 1")) {
74
75                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
76                         dbesc(datetime_convert()),
77                         intval(getmypid()),
78                         intval($r[0]["id"]));
79
80                 // Assure that there are no tasks executed twice
81                 $id = q("SELECT `id` FROM `workerqueue` WHERE `id` = %d AND `pid` = %d",
82                         intval($r[0]["id"]),
83                         intval(getmypid()));
84                 if (!$id) {
85                         logger("Queue item ".$r[0]["id"]." was executed multiple times - skip this execution", LOGGER_DEBUG);
86                         continue;
87                 }
88
89                 $argv = json_decode($r[0]["parameter"]);
90
91                 $argc = count($argv);
92
93                 // Check for existance and validity of the include file
94                 $include = $argv[0];
95
96                 if (!validate_include($include)) {
97                         logger("Include file ".$argv[0]." is not valid!");
98                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
99                         continue;
100                 }
101
102                 require_once($include);
103
104                 $funcname=str_replace(".php", "", basename($argv[0]))."_run";
105
106                 if (function_exists($funcname)) {
107                         logger("Process ".getmypid().": ".$funcname." ".$r[0]["parameter"]);
108                         $funcname($argv, $argc);
109
110                         logger("Process ".getmypid().": ".$funcname." - done");
111
112                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
113                 } else
114                         logger("Function ".$funcname." does not exist");
115
116                 // Quit the poller once every hour
117                 if (time() > ($starttime + 3600))
118                         return;
119
120                 // Count active workers and compare them with a maximum value that depends on the load
121                 if (poller_too_much_workers(3))
122                         return;
123         }
124
125 }
126
127 function poller_too_much_workers($stage) {
128
129         $queues = get_config("system", "worker_queues");
130
131         if ($queues == 0)
132                 $queues = 4;
133
134         $active = poller_active_workers();
135
136         // Decrease the number of workers at higher load
137         if(function_exists('sys_getloadavg')) {
138                 $load = max(sys_getloadavg());
139
140                 $maxsysload = intval(get_config('system','maxloadavg'));
141                 if($maxsysload < 1)
142                         $maxsysload = 50;
143
144                 $maxworkers = $queues;
145
146                 // Some magical mathemathics to reduce the workers
147                 $exponent = 3;
148                 $slope = $maxworkers / pow($maxsysload, $exponent);
149                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
150
151                 logger("Current load stage ".$stage.": ".$load." - maximum: ".$maxsysload." - current queues: ".$active." - maximum: ".$queues, LOGGER_DEBUG);
152
153         }
154
155         return($active >= $queues);
156 }
157
158 function poller_active_workers() {
159         $workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
160
161         return($workers[0]["workers"]);
162 }
163
164 if (array_search(__file__,get_included_files())===0){
165   poller_run($_SERVER["argv"],$_SERVER["argc"]);
166   killme();
167 }
168 ?>