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