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