]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Worker: Reserve some space for low priority processes
[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 use \Friendica\Core\Config;
14 use \Friendica\Core\PConfig;
15
16 require_once("boot.php");
17
18 function poller_run(&$argv, &$argc){
19         global $a, $db;
20
21         if(is_null($a)) {
22                 $a = new App;
23         }
24
25         if(is_null($db)) {
26                 @include(".htconfig.php");
27                 require_once("include/dba.php");
28                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
29                 unset($db_host, $db_user, $db_pass, $db_data);
30         };
31
32         // Quit when in maintenance
33         if (get_config('system', 'maintenance', true))
34                 return;
35
36         $a->start_process();
37
38         $mypid = getmypid();
39
40         if ($a->max_processes_reached())
41                 return;
42
43         if (poller_max_connections_reached())
44                 return;
45
46         if (App::maxload_reached())
47                 return;
48
49         // Checking the number of workers
50         if (poller_too_much_workers()) {
51                 poller_kill_stale_workers();
52                 return;
53         }
54
55         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
56                 // Run the cron job that calls all other jobs
57                 proc_run(PRIORITY_MEDIUM, "include/cron.php");
58
59                 // Run the cronhooks job separately from cron for being able to use a different timing
60                 proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
61
62                 // Cleaning dead processes
63                 poller_kill_stale_workers();
64         } else
65                 // Sleep four seconds before checking for running processes again to avoid having too many workers
66                 sleep(4);
67
68         // Checking number of workers
69         if (poller_too_much_workers())
70                 return;
71
72         $cooldown = Config::get("system", "worker_cooldown", 0);
73
74         $starttime = time();
75
76         while ($r = poller_worker_process()) {
77
78                 // Quit when in maintenance
79                 if (get_config('system', 'maintenance', true))
80                         return;
81
82                 // Constantly check the number of parallel database processes
83                 if ($a->max_processes_reached())
84                         return;
85
86                 // Constantly check the number of available database connections to let the frontend be accessible at any time
87                 if (poller_max_connections_reached())
88                         return;
89
90                 // Count active workers and compare them with a maximum value that depends on the load
91                 if (poller_too_much_workers())
92                         return;
93
94                 q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
95                         dbesc(datetime_convert()),
96                         intval($mypid),
97                         intval($r[0]["id"]));
98
99                 // Assure that there are no tasks executed twice
100                 $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
101                 if (!$id) {
102                         logger("Queue item ".$r[0]["id"]." vanished - skip this execution", LOGGER_DEBUG);
103                         continue;
104                 } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
105                         logger("Entry for queue item ".$r[0]["id"]." wasn't stored - we better stop here", LOGGER_DEBUG);
106                         return;
107                 } elseif ($id[0]["pid"] != $mypid) {
108                         logger("Queue item ".$r[0]["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
109                         continue;
110                 }
111
112                 $argv = json_decode($r[0]["parameter"]);
113
114                 $argc = count($argv);
115
116                 // Check for existance and validity of the include file
117                 $include = $argv[0];
118
119                 if (!validate_include($include)) {
120                         logger("Include file ".$argv[0]." is not valid!");
121                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
122                         continue;
123                 }
124
125                 require_once($include);
126
127                 $funcname = str_replace(".php", "", basename($argv[0]))."_run";
128
129                 if (function_exists($funcname)) {
130                         logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
131
132                         // For better logging create a new process id for every worker call
133                         // But preserve the old one for the worker
134                         $old_process_id = $a->process_id;
135                         $a->process_id = uniqid("wrk", true);
136
137                         $funcname($argv, $argc);
138
139                         $a->process_id = $old_process_id;
140
141                         if ($cooldown > 0) {
142                                 logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
143                                 sleep($cooldown);
144                         }
145
146                         logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
147
148                         q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
149                 } else
150                         logger("Function ".$funcname." does not exist");
151
152                 // Quit the poller once every hour
153                 if (time() > ($starttime + 3600))
154                         return;
155         }
156
157 }
158
159 /**
160  * @brief Checks if the number of database connections has reached a critical limit.
161  *
162  * @return bool Are more than 3/4 of the maximum connections used?
163  */
164 function poller_max_connections_reached() {
165
166         // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
167         $max = get_config("system", "max_connections");
168
169         // Fetch the percentage level where the poller will get active
170         $maxlevel = get_config("system", "max_connections_level");
171         if ($maxlevel == 0)
172                 $maxlevel = 75;
173
174         if ($max == 0) {
175                 // the maximum number of possible user connections can be a system variable
176                 $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
177                 if ($r)
178                         $max = $r[0]["Value"];
179
180                 // Or it can be granted. This overrides the system variable
181                 $r = q("SHOW GRANTS");
182                 if ($r)
183                         foreach ($r AS $grants) {
184                                 $grant = array_pop($grants);
185                                 if (stristr($grant, "GRANT USAGE ON"))
186                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match))
187                                                 $max = $match[1];
188                         }
189         }
190
191         // If $max is set we will use the processlist to determine the current number of connections
192         // The processlist only shows entries of the current user
193         if ($max != 0) {
194                 $r = q("SHOW PROCESSLIST");
195                 if (!$r)
196                         return false;
197
198                 $used = count($r);
199
200                 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
201
202                 $level = ($used / $max) * 100;
203
204                 if ($level >= $maxlevel) {
205                         logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
206                         return true;
207                 }
208         }
209
210         // We will now check for the system values.
211         // This limit could be reached although the user limits are fine.
212         $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
213         if (!$r)
214                 return false;
215
216         $max = intval($r[0]["Value"]);
217         if ($max == 0)
218                 return false;
219
220         $r = q("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
221         if (!$r)
222                 return false;
223
224         $used = intval($r[0]["Value"]);
225         if ($used == 0)
226                 return false;
227
228         logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
229
230         $level = $used / $max * 100;
231
232         if ($level < $maxlevel)
233                 return false;
234
235         logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
236         return true;
237 }
238
239 /**
240  * @brief fix the queue entry if the worker process died
241  *
242  */
243 function poller_kill_stale_workers() {
244         $r = q("SELECT `pid`, `executed`, `priority`, `parameter` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
245
246         if (!dbm::is_result($r)) {
247                 // No processing here needed
248                 return;
249         }
250
251         foreach($r AS $pid)
252                 if (!posix_kill($pid["pid"], 0))
253                         q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `pid` = 0 WHERE `pid` = %d",
254                                 intval($pid["pid"]));
255                 else {
256                         // Kill long running processes
257
258                         // Check if the priority is in a valid range
259                         if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE)))
260                                 $pid["priority"] = PRIORITY_MEDIUM;
261
262                         // Define the maximum durations
263                         $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
264                         $max_duration = $max_duration_defaults[$pid["priority"]];
265
266                         $argv = json_decode($pid["parameter"]);
267                         $argv[0] = basename($argv[0]);
268
269                         // How long is the process already running?
270                         $duration = (time() - strtotime($pid["executed"])) / 60;
271                         if ($duration > $max_duration) {
272                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
273                                 posix_kill($pid["pid"], SIGTERM);
274
275                                 // We killed the stale process.
276                                 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
277                                 // Additionally we are lowering the priority.
278                                 q("UPDATE `workerqueue` SET `executed` = '0000-00-00 00:00:00', `created` = '%s',
279                                                         `priority` = %d, `pid` = 0 WHERE `pid` = %d",
280                                         dbesc(datetime_convert()),
281                                         intval(PRIORITY_NEGLIGIBLE),
282                                         intval($pid["pid"]));
283                         } else
284                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
285                 }
286 }
287
288 function poller_too_much_workers() {
289
290
291         $queues = get_config("system", "worker_queues");
292
293         if ($queues == 0)
294                 $queues = 4;
295
296         $maxqueues = $queues;
297
298         $active = poller_active_workers();
299
300         // Decrease the number of workers at higher load
301         $load = current_load();
302         if($load) {
303                 $maxsysload = intval(get_config('system','maxloadavg'));
304                 if($maxsysload < 1)
305                         $maxsysload = 50;
306
307                 $maxworkers = $queues;
308
309                 // Some magical mathemathics to reduce the workers
310                 $exponent = 3;
311                 $slope = $maxworkers / pow($maxsysload, $exponent);
312                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
313
314                 $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00'");
315                 $entries = $s[0]["total"];
316
317                 if (Config::get("system", "worker_fastlane", false) AND ($queues > 0) AND ($entries > 0) AND ($active >= $queues)) {
318                         $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority` LIMIT 1");
319                         $top_priority = $s[0]["priority"];
320
321                         $s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` != '0000-00-00 00:00:00' LIMIT 1",
322                                 intval($top_priority));
323                         $high_running = dbm::is_result($s);
324
325                         if (!$high_running AND ($top_priority > PRIORITY_UNDEFINED) AND ($top_priority < PRIORITY_NEGLIGIBLE)) {
326                                 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
327                                 $queues = $active + 1;
328                         }
329                 }
330
331                 logger("Current load: ".$load." - maximum: ".$maxsysload." - current queues: ".$active."/".$entries." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
332
333                 // Are there fewer workers running as possible? Then fork a new one.
334                 if (!get_config("system", "worker_dont_fork") AND ($queues > ($active + 1)) AND ($entries > 1)) {
335                         logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
336                         $args = array("php", "include/poller.php", "no_cron");
337                         $a = get_app();
338                         $a->proc_run($args);
339                 }
340         }
341
342         return($active >= $queues);
343 }
344
345 function poller_active_workers() {
346         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
347
348         return($workers[0]["processes"]);
349 }
350
351 /**
352  * @brief Check if we should pass some slow processes
353  *
354  * When the active processes of the highest priority are using more than 2/3
355  * of all processes, we let pass slower processes.
356  *
357  * @param string $highest_priority Returns the currently highest priority
358  * @return bool We let pass a slower process than $highest_priority
359  */
360 function poller_passing_slow(&$highest_priority) {
361
362         $highest_priority = 0;
363
364         $r = q("SELECT `priority`
365                 FROM `process`
366                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid`
367                 WHERE `process`.`command` = 'poller.php'");
368
369         // No active processes at all? Fine
370         if (!dbm::is_result($r))
371                 return(false);
372
373         $priorities = array();
374         foreach ($r AS $line)
375                 $priorities[] = $line["priority"];
376
377         // Should not happen
378         if (count($priorities) == 0)
379                 return(false);
380
381         $highest_priority = min($priorities);
382
383         // The highest process is already the slowest one?
384         // Then we quit
385         if ($highest_priority == PRIORITY_NEGLIGIBLE)
386                 return(false);
387
388         $high = 0;
389         foreach ($priorities AS $priority)
390                 if ($priority == $highest_priority)
391                         ++$high;
392
393         logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
394         $passing_slow = (($high/count($priorities)) > (2/3));
395
396         if ($passing_slow)
397                 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
398
399         return($passing_slow);
400 }
401
402 /**
403  * @brief Returns the next worker process
404  *
405  * @return string SQL statement
406  */
407
408 function poller_worker_process() {
409
410         // Check if we should pass some low priority process
411         $highest_priority = 0;
412
413         if (poller_passing_slow($highest_priority)) {
414                 // Are there waiting processes with a higher priority than the currently highest?
415                 $r = q("SELECT * FROM `workerqueue`
416                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` < %d
417                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
418                 if (dbm::is_result($r))
419                         return $r;
420
421                 // Give slower processes some processing time
422                 $r = q("SELECT * FROM `workerqueue`
423                                 WHERE `executed` = '0000-00-00 00:00:00' AND `priority` > %d
424                                 ORDER BY `priority`, `created` LIMIT 1", dbesc($highest_priority));
425         }
426
427         // If there is no result (or we shouldn't pass lower processes) we check without priority limit
428         if (($highest_priority == 0) OR !dbm::is_result($r))
429                 $r = q("SELECT * FROM `workerqueue` WHERE `executed` = '0000-00-00 00:00:00' ORDER BY `priority`, `created` LIMIT 1");
430
431         return $r;
432 }
433
434 if (array_search(__file__,get_included_files())===0){
435         poller_run($_SERVER["argv"],$_SERVER["argc"]);
436
437         get_app()->end_process();
438
439         killme();
440 }
441 ?>