]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge remote-tracking branch 'upstream/develop' into 1702-null-date
[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
15 require_once("boot.php");
16
17 function poller_run($argv, $argc){
18         global $a, $db;
19
20         if(is_null($a)) {
21                 $a = new App;
22         }
23
24         if(is_null($db)) {
25                 @include(".htconfig.php");
26                 require_once("include/dba.php");
27                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
28                 unset($db_host, $db_user, $db_pass, $db_data);
29         };
30
31         Config::load();
32
33         // Quit when in maintenance
34         if (Config::get('system', 'maintenance', true)) {
35                 return;
36         }
37
38         $a->start_process();
39
40         if (poller_max_connections_reached()) {
41                 return;
42         }
43
44         if ($a->maxload_reached()) {
45                 return;
46         }
47
48         if(($argc <= 1) OR ($argv[1] != "no_cron")) {
49                 poller_run_cron();
50         }
51
52         if ($a->max_processes_reached()) {
53                 return;
54         }
55
56         // Checking the number of workers
57         if (poller_too_much_workers()) {
58                 poller_kill_stale_workers();
59                 return;
60         }
61
62         $starttime = time();
63
64         while ($r = poller_worker_process()) {
65
66                 // Count active workers and compare them with a maximum value that depends on the load
67                 if (poller_too_much_workers()) {
68                         return;
69                 }
70
71                 if (!poller_execute($r[0])) {
72                         return;
73                 }
74
75                 // Quit the poller once every hour
76                 if (time() > ($starttime + 3600))
77                         return;
78         }
79
80 }
81
82 /**
83  * @brief Execute a worker entry
84  *
85  * @param array $queue Workerqueue entry
86  *
87  * @return boolean "true" if further processing should be stopped
88  */
89 function poller_execute($queue) {
90
91         $a = get_app();
92
93         $mypid = getmypid();
94
95         // Quit when in maintenance
96         if (Config::get('system', 'maintenance', true)) {
97                 return false;
98         }
99
100         // Constantly check the number of parallel database processes
101         if ($a->max_processes_reached()) {
102                 return false;
103         }
104
105         // Constantly check the number of available database connections to let the frontend be accessible at any time
106         if (poller_max_connections_reached()) {
107                 return false;
108         }
109
110         $upd = q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `pid` = 0",
111                 dbesc(datetime_convert()),
112                 intval($mypid),
113                 intval($queue["id"]));
114
115         if (!$upd) {
116                 logger("Couldn't update queue entry ".$queue["id"]." - skip this execution", LOGGER_DEBUG);
117                 q("COMMIT");
118                 return true;
119         }
120
121         // Assure that there are no tasks executed twice
122         $id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
123         if (!$id) {
124                 logger("Queue item ".$queue["id"]." vanished - skip this execution", LOGGER_DEBUG);
125                 q("COMMIT");
126                 return true;
127         } elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
128                 logger("Entry for queue item ".$queue["id"]." wasn't stored - skip this execution", LOGGER_DEBUG);
129                 q("COMMIT");
130                 return true;
131         } elseif ($id[0]["pid"] != $mypid) {
132                 logger("Queue item ".$queue["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
133                 q("COMMIT");
134                 return true;
135         }
136         q("COMMIT");
137
138         $argv = json_decode($queue["parameter"]);
139
140         // Check for existance and validity of the include file
141         $include = $argv[0];
142
143         if (!validate_include($include)) {
144                 logger("Include file ".$argv[0]." is not valid!");
145                 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
146                 return true;
147         }
148
149         require_once($include);
150
151         $funcname = str_replace(".php", "", basename($argv[0]))."_run";
152
153         if (function_exists($funcname)) {
154
155                 poller_exec_function($queue, $funcname, $argv);
156
157                 q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($queue["id"]));
158         } else {
159                 logger("Function ".$funcname." does not exist");
160         }
161
162         return true;
163 }
164
165 /**
166  * @brief Execute a function from the queue
167  *
168  * @param array $queue Workerqueue entry
169  * @param string $funcname name of the function
170  * @param array $argv Array of values to be passed to the function
171  */
172 function poller_exec_function($queue, $funcname, $argv) {
173
174         $a = get_app();
175
176         $mypid = getmypid();
177
178         $argc = count($argv);
179
180         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]);
181
182         $stamp = (float)microtime(true);
183
184         // We use the callstack here to analyze the performance of executed worker entries.
185         // For this reason the variables have to be initialized.
186         if (Config::get("system", "profiler")) {
187                 $a->performance["start"] = microtime(true);
188                 $a->performance["database"] = 0;
189                 $a->performance["database_write"] = 0;
190                 $a->performance["network"] = 0;
191                 $a->performance["file"] = 0;
192                 $a->performance["rendering"] = 0;
193                 $a->performance["parser"] = 0;
194                 $a->performance["marktime"] = 0;
195                 $a->performance["markstart"] = microtime(true);
196                 $a->callstack = array();
197         }
198
199         // For better logging create a new process id for every worker call
200         // But preserve the old one for the worker
201         $old_process_id = $a->process_id;
202         $a->process_id = uniqid("wrk", true);
203
204         $funcname($argv, $argc);
205
206         $a->process_id = $old_process_id;
207
208         $duration = number_format(microtime(true) - $stamp, 3);
209
210         if ($duration > 3600) {
211                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
212         } elseif ($duration > 600) {
213                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
214         } elseif ($duration > 300) {
215                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
216         } elseif ($duration > 120) {
217                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
218         }
219
220         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds.");
221
222         // Write down the performance values into the log
223         if (Config::get("system", "profiler")) {
224                 $duration = microtime(true)-$a->performance["start"];
225
226                 if (Config::get("rendertime", "callstack")) {
227                         if (isset($a->callstack["database"])) {
228                                 $o = "\nDatabase Read:\n";
229                                 foreach ($a->callstack["database"] AS $func => $time) {
230                                         $time = round($time, 3);
231                                         if ($time > 0)
232                                                 $o .= $func.": ".$time."\n";
233                                 }
234                         }
235                         if (isset($a->callstack["database_write"])) {
236                                 $o .= "\nDatabase Write:\n";
237                                 foreach ($a->callstack["database_write"] AS $func => $time) {
238                                         $time = round($time, 3);
239                                         if ($time > 0)
240                                                 $o .= $func.": ".$time."\n";
241                                 }
242                         }
243                         if (isset($a->callstack["network"])) {
244                                 $o .= "\nNetwork:\n";
245                                 foreach ($a->callstack["network"] AS $func => $time) {
246                                         $time = round($time, 3);
247                                         if ($time > 0)
248                                                 $o .= $func.": ".$time."\n";
249                                 }
250                         }
251                 } else {
252                         $o = '';
253                 }
254
255                 logger("ID ".$queue["id"].": ".$funcname.": ".sprintf("DB: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
256                         number_format($a->performance["database"] - $a->performance["database_write"], 2),
257                         number_format($a->performance["database_write"], 2),
258                         number_format($a->performance["network"], 2),
259                         number_format($a->performance["file"], 2),
260                         number_format($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2),
261                         number_format($duration, 2)),
262                         LOGGER_DEBUG);
263         }
264
265         $cooldown = Config::get("system", "worker_cooldown", 0);
266
267         if ($cooldown > 0) {
268                 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
269                 sleep($cooldown);
270         }
271 }
272
273 /**
274  * @brief Checks if the number of database connections has reached a critical limit.
275  *
276  * @return bool Are more than 3/4 of the maximum connections used?
277  */
278 function poller_max_connections_reached() {
279
280         // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
281         $max = Config::get("system", "max_connections");
282
283         // Fetch the percentage level where the poller will get active
284         $maxlevel = Config::get("system", "max_connections_level", 75);
285
286         if ($max == 0) {
287                 // the maximum number of possible user connections can be a system variable
288                 $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
289                 if ($r)
290                         $max = $r[0]["Value"];
291
292                 // Or it can be granted. This overrides the system variable
293                 $r = q("SHOW GRANTS");
294                 if ($r)
295                         foreach ($r AS $grants) {
296                                 $grant = array_pop($grants);
297                                 if (stristr($grant, "GRANT USAGE ON"))
298                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match))
299                                                 $max = $match[1];
300                         }
301         }
302
303         // If $max is set we will use the processlist to determine the current number of connections
304         // The processlist only shows entries of the current user
305         if ($max != 0) {
306                 $r = q("SHOW PROCESSLIST");
307                 if (!dbm::is_result($r))
308                         return false;
309
310                 $used = count($r);
311
312                 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
313
314                 $level = ($used / $max) * 100;
315
316                 if ($level >= $maxlevel) {
317                         logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
318                         return true;
319                 }
320         }
321
322         // We will now check for the system values.
323         // This limit could be reached although the user limits are fine.
324         $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
325         if (!$r)
326                 return false;
327
328         $max = intval($r[0]["Value"]);
329         if ($max == 0)
330                 return false;
331
332         $r = q("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
333         if (!$r)
334                 return false;
335
336         $used = intval($r[0]["Value"]);
337         if ($used == 0)
338                 return false;
339
340         logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
341
342         $level = $used / $max * 100;
343
344         if ($level < $maxlevel)
345                 return false;
346
347         logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
348         return true;
349 }
350
351 /**
352  * @brief fix the queue entry if the worker process died
353  *
354  */
355 function poller_kill_stale_workers() {
356         $r = q("SELECT `pid`, `executed`, `priority`, `parameter` FROM `workerqueue` WHERE `executed` > '%s'", dbesc(NULL_DATE));
357
358         if (!dbm::is_result($r)) {
359                 // No processing here needed
360                 return;
361         }
362
363         foreach($r AS $pid)
364                 if (!posix_kill($pid["pid"], 0))
365                         q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = 0 WHERE `pid` = %d",
366                                 dbesc(NULL_DATE), intval($pid["pid"]));
367                 else {
368                         // Kill long running processes
369
370                         // Check if the priority is in a valid range
371                         if (!in_array($pid["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE)))
372                                 $pid["priority"] = PRIORITY_MEDIUM;
373
374                         // Define the maximum durations
375                         $max_duration_defaults = array(PRIORITY_CRITICAL => 360, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 360);
376                         $max_duration = $max_duration_defaults[$pid["priority"]];
377
378                         $argv = json_decode($pid["parameter"]);
379                         $argv[0] = basename($argv[0]);
380
381                         // How long is the process already running?
382                         $duration = (time() - strtotime($pid["executed"])) / 60;
383                         if ($duration > $max_duration) {
384                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
385                                 posix_kill($pid["pid"], SIGTERM);
386
387                                 // We killed the stale process.
388                                 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
389                                 // Additionally we are lowering the priority.
390                                 q("UPDATE `workerqueue` SET `executed` = '%s', `created` = '%s',
391                                                         `priority` = %d, `pid` = 0 WHERE `pid` = %d",
392                                         dbesc(NULL_DATE),
393                                         dbesc(datetime_convert()),
394                                         intval(PRIORITY_NEGLIGIBLE),
395                                         intval($pid["pid"]));
396                         } else
397                                 logger("Worker process ".$pid["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
398                 }
399 }
400
401 /**
402  * @brief Checks if the number of active workers exceeds the given limits
403  *
404  * @return bool Are there too much workers running?
405  */
406 function poller_too_much_workers() {
407         $queues = Config::get("system", "worker_queues", 4);
408
409         $maxqueues = $queues;
410
411         $active = poller_active_workers();
412
413         // Decrease the number of workers at higher load
414         $load = current_load();
415         if($load) {
416                 $maxsysload = intval(Config::get("system", "maxloadavg", 50));
417
418                 $maxworkers = $queues;
419
420                 // Some magical mathemathics to reduce the workers
421                 $exponent = 3;
422                 $slope = $maxworkers / pow($maxsysload, $exponent);
423                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
424
425                 $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= '%s'", dbesc(NULL_DATE));
426                 $entries = $s[0]["total"];
427
428                 if (Config::get("system", "worker_fastlane", false) AND ($queues > 0) AND ($entries > 0) AND ($active >= $queues)) {
429                         $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority` LIMIT 1", dbesc(NULL_DATE));
430                         $top_priority = $s[0]["priority"];
431
432                         $s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` > '%s' LIMIT 1",
433                                 intval($top_priority), dbesc(NULL_DATE));
434                         $high_running = dbm::is_result($s);
435
436                         if (!$high_running AND ($top_priority > PRIORITY_UNDEFINED) AND ($top_priority < PRIORITY_NEGLIGIBLE)) {
437                                 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
438                                 $queues = $active + 1;
439                         }
440                 }
441
442                 // Create a list of queue entries grouped by their priority
443                 $running = array(PRIORITY_CRITICAL => 0,
444                                 PRIORITY_HIGH => 0,
445                                 PRIORITY_MEDIUM => 0,
446                                 PRIORITY_LOW => 0,
447                                 PRIORITY_NEGLIGIBLE => 0);
448
449                 $r = q("SELECT COUNT(*) AS `running`, `priority` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` GROUP BY `priority`");
450                 if (dbm::is_result($r))
451                         foreach ($r AS $process)
452                                 $running[$process["priority"]] = $process["running"];
453
454                 $processlist = "";
455                 $r = q("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` GROUP BY `priority`");
456                 if (dbm::is_result($r))
457                         foreach ($r as $entry) {
458                                 if ($processlist != "")
459                                         $processlist .= ", ";
460                                 $processlist .= $entry["priority"].":".$running[$entry["priority"]]."/".$entry["entries"];
461                         }
462
463                 logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries." (".$processlist.") - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
464
465                 // Are there fewer workers running as possible? Then fork a new one.
466                 if (!Config::get("system", "worker_dont_fork") AND ($queues > ($active + 1)) AND ($entries > 1)) {
467                         logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
468                         $args = array("php", "include/poller.php", "no_cron");
469                         $a = get_app();
470                         $a->proc_run($args);
471                 }
472         }
473
474         return($active >= $queues);
475 }
476
477 /**
478  * @brief Returns the number of active poller processes
479  *
480  * @return integer Number of active poller processes
481  */
482 function poller_active_workers() {
483         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
484
485         return($workers[0]["processes"]);
486 }
487
488 /**
489  * @brief Check if we should pass some slow processes
490  *
491  * When the active processes of the highest priority are using more than 2/3
492  * of all processes, we let pass slower processes.
493  *
494  * @param string $highest_priority Returns the currently highest priority
495  * @return bool We let pass a slower process than $highest_priority
496  */
497 function poller_passing_slow(&$highest_priority) {
498
499         $highest_priority = 0;
500
501         $r = q("SELECT `priority`
502                 FROM `process`
503                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid`");
504
505         // No active processes at all? Fine
506         if (!dbm::is_result($r))
507                 return(false);
508
509         $priorities = array();
510         foreach ($r AS $line)
511                 $priorities[] = $line["priority"];
512
513         // Should not happen
514         if (count($priorities) == 0)
515                 return(false);
516
517         $highest_priority = min($priorities);
518
519         // The highest process is already the slowest one?
520         // Then we quit
521         if ($highest_priority == PRIORITY_NEGLIGIBLE)
522                 return(false);
523
524         $high = 0;
525         foreach ($priorities AS $priority)
526                 if ($priority == $highest_priority)
527                         ++$high;
528
529         logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
530         $passing_slow = (($high/count($priorities)) > (2/3));
531
532         if ($passing_slow)
533                 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
534
535         return($passing_slow);
536 }
537
538 /**
539  * @brief Returns the next worker process
540  *
541  * @return string SQL statement
542  */
543 function poller_worker_process() {
544
545         q("START TRANSACTION;");
546
547         // Check if we should pass some low priority process
548         $highest_priority = 0;
549
550         if (poller_passing_slow($highest_priority)) {
551                 // Are there waiting processes with a higher priority than the currently highest?
552                 $r = q("SELECT * FROM `workerqueue`
553                                 WHERE `executed` <= '%s' AND `priority` < %d
554                                 ORDER BY `priority`, `created` LIMIT 1",
555                                 dbesc(NULL_DATE),
556                                 intval($highest_priority));
557                 if (dbm::is_result($r))
558                         return $r;
559
560                 // Give slower processes some processing time
561                 $r = q("SELECT * FROM `workerqueue`
562                                 WHERE `executed` <= '%s' AND `priority` > %d
563                                 ORDER BY `priority`, `created` LIMIT 1",
564                                 dbesc(NULL_DATE),
565                                 intval($highest_priority));
566         }
567
568         // If there is no result (or we shouldn't pass lower processes) we check without priority limit
569         if (($highest_priority == 0) OR !dbm::is_result($r))
570                 $r = q("SELECT * FROM `workerqueue` WHERE `executed` <= '%s' ORDER BY `priority`, `created` LIMIT 1", dbesc(NULL_DATE));
571
572         return $r;
573 }
574
575 /**
576  * @brief Call the front end worker
577  */
578 function call_worker() {
579         if (!Config::get("system", "frontend_worker") OR !Config::get("system", "worker")) {
580                 return;
581         }
582
583         $url = App::get_baseurl()."/worker";
584         fetch_url($url, false, $redirects, 1);
585 }
586
587 /**
588  * @brief Call the front end worker if there aren't any active
589  */
590 function call_worker_if_idle() {
591         if (!Config::get("system", "frontend_worker") OR !Config::get("system", "worker")) {
592                 return;
593         }
594
595         // Do we have "proc_open"? Then we can fork the poller
596         if (function_exists("proc_open")) {
597                 // When was the last time that we called the worker?
598                 // Less than one minute? Then we quit
599                 if ((time() - Config::get("system", "worker_started")) < 60) {
600                         return;
601                 }
602
603                 set_config("system", "worker_started", time());
604
605                 // Do we have enough running workers? Then we quit here.
606                 if (poller_too_much_workers()) {
607                         // Cleaning dead processes
608                         poller_kill_stale_workers();
609                         get_app()->remove_inactive_processes();
610
611                         return;
612                 }
613
614                 poller_run_cron();
615
616                 logger('Call poller', LOGGER_DEBUG);
617
618                 $args = array("php", "include/poller.php", "no_cron");
619                 $a = get_app();
620                 $a->proc_run($args);
621                 return;
622         }
623
624         // We cannot execute background processes.
625         // We now run the processes from the frontend.
626         // This won't work with long running processes.
627         poller_run_cron();
628
629         clear_worker_processes();
630
631         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
632
633         if ($workers[0]["processes"] == 0) {
634                 call_worker();
635         }
636 }
637
638 /**
639  * @brief Removes long running worker processes
640  */
641 function clear_worker_processes() {
642         $timeout = Config::get("system", "frontend_worker_timeout", 10);
643
644         /// @todo We should clean up the corresponding workerqueue entries as well
645         q("DELETE FROM `process` WHERE `created` < '%s' AND `command` = 'worker.php'",
646                 dbesc(datetime_convert('UTC','UTC',"now - ".$timeout." minutes")));
647 }
648
649 /**
650  * @brief Runs the cron processes
651  */
652 function poller_run_cron() {
653         logger('Add cron entries', LOGGER_DEBUG);
654
655         // Check for spooled items
656         proc_run(PRIORITY_HIGH, "include/spool_post.php");
657
658         // Run the cron job that calls all other jobs
659         proc_run(PRIORITY_MEDIUM, "include/cron.php");
660
661         // Run the cronhooks job separately from cron for being able to use a different timing
662         proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
663
664         // Cleaning dead processes
665         poller_kill_stale_workers();
666 }
667
668 if (array_search(__file__,get_included_files())===0){
669         poller_run($_SERVER["argv"],$_SERVER["argc"]);
670
671         get_app()->end_process();
672
673         killme();
674 }
675 ?>