]> git.mxchange.org Git - friendica.git/blob - include/poller.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / include / poller.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Core\Config;
6 use Friendica\Util\Lock;
7
8 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
9         $directory = dirname($_SERVER["argv"][0]);
10
11         if (substr($directory, 0, 1) != "/") {
12                 $directory = $_SERVER["PWD"]."/".$directory;
13         }
14         $directory = realpath($directory."/..");
15
16         chdir($directory);
17 }
18
19 require_once("boot.php");
20
21 function poller_run($argv, $argc){
22         global $a, $db, $poller_up_start, $poller_db_duration;
23
24         $poller_up_start = microtime(true);
25
26         $a = new App(dirname(__DIR__));
27
28         @include(".htconfig.php");
29         require_once("include/dba.php");
30         $db = new dba($db_host, $db_user, $db_pass, $db_data);
31         unset($db_host, $db_user, $db_pass, $db_data);
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         // At first check the maximum load. We shouldn't continue with a high load
45         if ($a->maxload_reached()) {
46                 logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG);
47                 return;
48         }
49
50         // We now start the process. This is done after the load check since this could increase the load.
51         $a->start_process();
52
53         // Kill stale processes every 5 minutes
54         $last_cleanup = Config::get('system', 'poller_last_cleaned', 0);
55         if (time() > ($last_cleanup + 300)) {
56                 Config::set('system', 'poller_last_cleaned', time());
57                 poller_kill_stale_workers();
58         }
59
60         // Count active workers and compare them with a maximum value that depends on the load
61         if (poller_too_much_workers()) {
62                 logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
63                 return;
64         }
65
66         // Do we have too few memory?
67         if ($a->min_memory_reached()) {
68                 logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
69                 return;
70         }
71
72         // Possibly there are too much database connections
73         if (poller_max_connections_reached()) {
74                 logger('Pre check: maximum connections reached, quitting.', LOGGER_DEBUG);
75                 return;
76         }
77
78         // Possibly there are too much database processes that block the system
79         if ($a->max_processes_reached()) {
80                 logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG);
81                 return;
82         }
83
84         // Now we start additional cron processes if we should do so
85         if (($argc <= 1) || ($argv[1] != "no_cron")) {
86                 poller_run_cron();
87         }
88
89         $starttime = time();
90
91         // We fetch the next queue entry that is about to be executed
92         while ($r = poller_worker_process($passing_slow)) {
93
94                 // When we are processing jobs with a lower priority, we don't refetch new jobs
95                 // Otherwise fast jobs could wait behind slow ones and could be blocked.
96                 $refetched = $passing_slow;
97
98                 foreach ($r AS $entry) {
99                         // Assure that the priority is an integer value
100                         $entry['priority'] = (int)$entry['priority'];
101
102                         // The work will be done
103                         if (!poller_execute($entry)) {
104                                 logger('Process execution failed, quitting.', LOGGER_DEBUG);
105                                 return;
106                         }
107
108                         // If possible we will fetch new jobs for this worker
109                         if (!$refetched && Lock::set('poller_worker_process', 0)) {
110                                 $stamp = (float)microtime(true);
111                                 $refetched = find_worker_processes($passing_slow);
112                                 $poller_db_duration += (microtime(true) - $stamp);
113                                 Lock::remove('poller_worker_process');
114                         }
115                 }
116
117                 // To avoid the quitting of multiple pollers only one poller at a time will execute the check
118                 if (Lock::set('poller_worker', 0)) {
119                         $stamp = (float)microtime(true);
120                         // Count active workers and compare them with a maximum value that depends on the load
121                         if (poller_too_much_workers()) {
122                                 logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
123                                 return;
124                         }
125
126                         // Check free memory
127                         if ($a->min_memory_reached()) {
128                                 logger('Memory limit reached, quitting.', LOGGER_DEBUG);
129                                 return;
130                         }
131                         Lock::remove('poller_worker');
132                         $poller_db_duration += (microtime(true) - $stamp);
133                 }
134
135                 // Quit the poller once every 5 minutes
136                 if (time() > ($starttime + 300)) {
137                         logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
138                         return;
139                 }
140         }
141         logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG);
142 }
143
144 /**
145  * @brief Returns the number of non executed entries in the worker queue
146  *
147  * @return integer Number of non executed entries in the worker queue
148  */
149 function poller_total_entries() {
150         $s = q("SELECT COUNT(*) AS `total` FROM `workerqueue` WHERE `executed` <= '%s' AND NOT `done`", dbesc(NULL_DATE));
151         if (dbm::is_result($s)) {
152                 return $s[0]["total"];
153         } else {
154                 return 0;
155         }
156 }
157
158 /**
159  * @brief Returns the highest priority in the worker queue that isn't executed
160  *
161  * @return integer Number of active poller processes
162  */
163 function poller_highest_priority() {
164         $s = q("SELECT `priority` FROM `workerqueue` WHERE `executed` <= '%s' AND NOT `done` ORDER BY `priority` LIMIT 1", dbesc(NULL_DATE));
165         if (dbm::is_result($s)) {
166                 return $s[0]["priority"];
167         } else {
168                 return 0;
169         }
170 }
171
172 /**
173  * @brief Returns if a process with the given priority is running
174  *
175  * @param integer $priority The priority that should be checked
176  *
177  * @return integer Is there a process running with that priority?
178  */
179 function poller_process_with_priority_active($priority) {
180         $s = q("SELECT `id` FROM `workerqueue` WHERE `priority` <= %d AND `executed` > '%s' AND NOT `done` LIMIT 1",
181                         intval($priority), dbesc(NULL_DATE));
182         return dbm::is_result($s);
183 }
184
185 /**
186  * @brief Execute a worker entry
187  *
188  * @param array $queue Workerqueue entry
189  *
190  * @return boolean "true" if further processing should be stopped
191  */
192 function poller_execute($queue) {
193         global $poller_db_duration, $poller_last_update;
194
195         $a = get_app();
196
197         $mypid = getmypid();
198
199         // Quit when in maintenance
200         if (Config::get('system', 'maintenance', true)) {
201                 logger("Maintenance mode - quit process ".$mypid, LOGGER_DEBUG);
202                 return false;
203         }
204
205         // Constantly check the number of parallel database processes
206         if ($a->max_processes_reached()) {
207                 logger("Max processes reached for process ".$mypid, LOGGER_DEBUG);
208                 return false;
209         }
210
211         // Constantly check the number of available database connections to let the frontend be accessible at any time
212         if (poller_max_connections_reached()) {
213                 logger("Max connection reached for process ".$mypid, LOGGER_DEBUG);
214                 return false;
215         }
216
217         $argv = json_decode($queue["parameter"]);
218
219         // Check for existance and validity of the include file
220         $include = $argv[0];
221
222         if (!validate_include($include)) {
223                 logger("Include file ".$argv[0]." is not valid!");
224                 dba::delete('workerqueue', array('id' => $queue["id"]));
225                 return true;
226         }
227
228         require_once($include);
229
230         $funcname = str_replace(".php", "", basename($argv[0]))."_run";
231
232         if (function_exists($funcname)) {
233
234                 // We constantly update the "executed" date every minute to avoid being killed too soon
235                 if (!isset($poller_last_update)) {
236                         $poller_last_update = strtotime($queue["executed"]);
237                 }
238
239                 $age = (time() - $poller_last_update) / 60;
240                 $poller_last_update = time();
241
242                 if ($age > 1) {
243                         $stamp = (float)microtime(true);
244                         dba::update('workerqueue', array('executed' => datetime_convert()), array('pid' => $mypid, 'done' => false));
245                         $poller_db_duration += (microtime(true) - $stamp);
246                 }
247
248                 poller_exec_function($queue, $funcname, $argv);
249
250                 $stamp = (float)microtime(true);
251                 dba::update('workerqueue', array('done' => true), array('id' => $queue["id"]));
252                 $poller_db_duration = (microtime(true) - $stamp);
253         } else {
254                 logger("Function ".$funcname." does not exist");
255                 dba::delete('workerqueue', array('id' => $queue["id"]));
256         }
257
258         return true;
259 }
260
261 /**
262  * @brief Execute a function from the queue
263  *
264  * @param array $queue Workerqueue entry
265  * @param string $funcname name of the function
266  * @param array $argv Array of values to be passed to the function
267  */
268 function poller_exec_function($queue, $funcname, $argv) {
269         global $poller_up_start, $poller_db_duration, $poller_lock_duration;
270
271         $a = get_app();
272
273         $mypid = getmypid();
274
275         $argc = count($argv);
276
277         $new_process_id = uniqid("wrk", true);
278
279         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
280
281         $stamp = (float)microtime(true);
282
283         // We use the callstack here to analyze the performance of executed worker entries.
284         // For this reason the variables have to be initialized.
285         if (Config::get("system", "profiler")) {
286                 $a->performance["start"] = microtime(true);
287                 $a->performance["database"] = 0;
288                 $a->performance["database_write"] = 0;
289                 $a->performance["network"] = 0;
290                 $a->performance["file"] = 0;
291                 $a->performance["rendering"] = 0;
292                 $a->performance["parser"] = 0;
293                 $a->performance["marktime"] = 0;
294                 $a->performance["markstart"] = microtime(true);
295                 $a->callstack = array();
296         }
297
298         // For better logging create a new process id for every worker call
299         // But preserve the old one for the worker
300         $old_process_id = $a->process_id;
301         $a->process_id = $new_process_id;
302         $a->queue = $queue;
303
304         $up_duration = number_format(microtime(true) - $poller_up_start, 3);
305
306         // Reset global data to avoid interferences
307         unset($_SESSION);
308
309         $funcname($argv, $argc);
310
311         $a->process_id = $old_process_id;
312         unset($a->queue);
313
314         $duration = number_format(microtime(true) - $stamp, 3);
315
316         $poller_up_start = microtime(true);
317
318         /* With these values we can analyze how effective the worker is.
319          * The database and rest time should be low since this is the unproductive time.
320          * The execution time is the productive time.
321          * By changing parameters like the maximum number of workers we can check the effectivness.
322         */
323         logger('DB: '.number_format($poller_db_duration, 2).
324                 ' - Lock: '.number_format($poller_lock_duration, 2).
325                 ' - Rest: '.number_format($up_duration - $poller_db_duration - $poller_lock_duration, 2).
326                 ' - Execution: '.number_format($duration, 2), LOGGER_DEBUG);
327         $poller_lock_duration = 0;
328
329         if ($duration > 3600) {
330                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
331         } elseif ($duration > 600) {
332                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
333         } elseif ($duration > 300) {
334                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
335         } elseif ($duration > 120) {
336                 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
337         }
338
339         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds. Process PID: ".$new_process_id);
340
341         // Write down the performance values into the log
342         if (Config::get("system", "profiler")) {
343                 $duration = microtime(true)-$a->performance["start"];
344
345                 if (Config::get("rendertime", "callstack")) {
346                         if (isset($a->callstack["database"])) {
347                                 $o = "\nDatabase Read:\n";
348                                 foreach ($a->callstack["database"] AS $func => $time) {
349                                         $time = round($time, 3);
350                                         if ($time > 0) {
351                                                 $o .= $func.": ".$time."\n";
352                                         }
353                                 }
354                         }
355                         if (isset($a->callstack["database_write"])) {
356                                 $o .= "\nDatabase Write:\n";
357                                 foreach ($a->callstack["database_write"] AS $func => $time) {
358                                         $time = round($time, 3);
359                                         if ($time > 0) {
360                                                 $o .= $func.": ".$time."\n";
361                                         }
362                                 }
363                         }
364                         if (isset($a->callstack["network"])) {
365                                 $o .= "\nNetwork:\n";
366                                 foreach ($a->callstack["network"] AS $func => $time) {
367                                         $time = round($time, 3);
368                                         if ($time > 0) {
369                                                 $o .= $func.": ".$time."\n";
370                                         }
371                                 }
372                         }
373                 } else {
374                         $o = '';
375                 }
376
377                 logger("ID ".$queue["id"].": ".$funcname.": ".sprintf("DB: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
378                         number_format($a->performance["database"] - $a->performance["database_write"], 2),
379                         number_format($a->performance["database_write"], 2),
380                         number_format($a->performance["network"], 2),
381                         number_format($a->performance["file"], 2),
382                         number_format($duration - ($a->performance["database"] + $a->performance["network"] + $a->performance["file"]), 2),
383                         number_format($duration, 2)),
384                         LOGGER_DEBUG);
385         }
386
387         $cooldown = Config::get("system", "worker_cooldown", 0);
388
389         if ($cooldown > 0) {
390                 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
391                 sleep($cooldown);
392         }
393 }
394
395 /**
396  * @brief Checks if the number of database connections has reached a critical limit.
397  *
398  * @return bool Are more than 3/4 of the maximum connections used?
399  */
400 function poller_max_connections_reached() {
401
402         // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
403         $max = Config::get("system", "max_connections");
404
405         // Fetch the percentage level where the poller will get active
406         $maxlevel = Config::get("system", "max_connections_level", 75);
407
408         if ($max == 0) {
409                 // the maximum number of possible user connections can be a system variable
410                 $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
411                 if (dbm::is_result($r)) {
412                         $max = $r[0]["Value"];
413                 }
414                 // Or it can be granted. This overrides the system variable
415                 $r = q("SHOW GRANTS");
416                 if (dbm::is_result($r)) {
417                         foreach ($r AS $grants) {
418                                 $grant = array_pop($grants);
419                                 if (stristr($grant, "GRANT USAGE ON")) {
420                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match)) {
421                                                 $max = $match[1];
422                                         }
423                                 }
424                         }
425                 }
426         }
427
428         // If $max is set we will use the processlist to determine the current number of connections
429         // The processlist only shows entries of the current user
430         if ($max != 0) {
431                 $r = q("SHOW PROCESSLIST");
432                 if (!dbm::is_result($r)) {
433                         return false;
434                 }
435                 $used = count($r);
436
437                 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
438
439                 $level = ($used / $max) * 100;
440
441                 if ($level >= $maxlevel) {
442                         logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
443                         return true;
444                 }
445         }
446
447         // We will now check for the system values.
448         // This limit could be reached although the user limits are fine.
449         $r = q("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
450         if (!dbm::is_result($r)) {
451                 return false;
452         }
453         $max = intval($r[0]["Value"]);
454         if ($max == 0) {
455                 return false;
456         }
457         $r = q("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
458         if (!dbm::is_result($r)) {
459                 return false;
460         }
461         $used = intval($r[0]["Value"]);
462         if ($used == 0) {
463                 return false;
464         }
465         logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
466
467         $level = $used / $max * 100;
468
469         if ($level < $maxlevel) {
470                 return false;
471         }
472         logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
473         return true;
474 }
475
476 /**
477  * @brief fix the queue entry if the worker process died
478  *
479  */
480 function poller_kill_stale_workers() {
481         $entries = dba::select('workerqueue', array('id', 'pid', 'executed', 'priority', 'parameter'),
482                                 array('`executed` > ? AND NOT `done` AND `pid` != 0', NULL_DATE),
483                                 array('order' => array('priority', 'created')));
484         while ($entry = dba::fetch($entries)) {
485                 if (!posix_kill($entry["pid"], 0)) {
486                         dba::update('workerqueue', array('executed' => NULL_DATE, 'pid' => 0),
487                                         array('id' => $entry["id"]));
488                 } else {
489                         // Kill long running processes
490                         // Check if the priority is in a valid range
491                         if (!in_array($entry["priority"], array(PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE))) {
492                                 $entry["priority"] = PRIORITY_MEDIUM;
493                         }
494
495                         // Define the maximum durations
496                         $max_duration_defaults = array(PRIORITY_CRITICAL => 720, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 720);
497                         $max_duration = $max_duration_defaults[$entry["priority"]];
498
499                         $argv = json_decode($entry["parameter"]);
500                         $argv[0] = basename($argv[0]);
501
502                         // How long is the process already running?
503                         $duration = (time() - strtotime($entry["executed"])) / 60;
504                         if ($duration > $max_duration) {
505                                 logger("Worker process ".$entry["pid"]." (".implode(" ", $argv).") took more than ".$max_duration." minutes. It will be killed now.");
506                                 posix_kill($entry["pid"], SIGTERM);
507
508                                 // We killed the stale process.
509                                 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
510                                 // Additionally we are lowering the priority. (But not PRIORITY_CRITICAL)
511                                 if ($entry["priority"] == PRIORITY_HIGH) {
512                                         $new_priority = PRIORITY_MEDIUM;
513                                 } elseif ($entry["priority"] == PRIORITY_MEDIUM) {
514                                         $new_priority = PRIORITY_LOW;
515                                 } elseif ($entry["priority"] != PRIORITY_CRITICAL) {
516                                         $new_priority = PRIORITY_NEGLIGIBLE;
517                                 }
518                                 dba::update('workerqueue',
519                                                 array('executed' => NULL_DATE, 'created' => datetime_convert(), 'priority' => $new_priority, 'pid' => 0),
520                                                 array('id' => $entry["id"]));
521                         } else {
522                                 logger("Worker process ".$entry["pid"]." (".implode(" ", $argv).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
523                         }
524                 }
525         }
526 }
527
528 /**
529  * @brief Checks if the number of active workers exceeds the given limits
530  *
531  * @return bool Are there too much workers running?
532  */
533 function poller_too_much_workers() {
534         $queues = Config::get("system", "worker_queues", 4);
535
536         $maxqueues = $queues;
537
538         $active = poller_active_workers();
539
540         // Decrease the number of workers at higher load
541         $load = current_load();
542         if ($load) {
543                 $maxsysload = intval(Config::get("system", "maxloadavg", 50));
544
545                 $maxworkers = $queues;
546
547                 // Some magical mathemathics to reduce the workers
548                 $exponent = 3;
549                 $slope = $maxworkers / pow($maxsysload, $exponent);
550                 $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
551
552                 if (Config::get('system', 'worker_debug')) {
553                         // Create a list of queue entries grouped by their priority
554                         $listitem = array();
555
556                         // Adding all processes with no workerqueue entry
557                         $processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS
558                                                 (SELECT id FROM `workerqueue`
559                                                 WHERE `workerqueue`.`pid` = `process`.`pid` AND NOT `done` AND `pid` != ?)", getmypid());
560                         if ($process = dba::fetch($processes)) {
561                                 $listitem[0] = "0:".$process["running"];
562                         }
563                         dba::close($processes);
564
565                         // Now adding all processes with workerqueue entries
566                         $entries = dba::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` GROUP BY `priority`");
567                         while ($entry = dba::fetch($entries)) {
568                                 $processes = dba::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done` WHERE `priority` = ?", $entry["priority"]);
569                                 if ($process = dba::fetch($processes)) {
570                                         $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
571                                 }
572                                 dba::close($processes);
573                         }
574                         dba::close($entries);
575
576                         $intervals = array(1, 10, 60);
577                         $jobs_per_minute = array();
578                         foreach ($intervals AS $interval) {
579                                 $jobs = dba::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE");
580                                 if ($job = dba::fetch($jobs)) {
581                                         $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
582                                 }
583                                 dba::close($jobs);
584                         }
585                         $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')';
586                 }
587
588                 $entries = poller_total_entries();
589
590                 if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
591                         $top_priority = poller_highest_priority();
592                         $high_running = poller_process_with_priority_active($top_priority);
593
594                         if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
595                                 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
596                                 $queues = $active + 1;
597                         }
598                 }
599
600                 logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries.$processlist." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
601
602                 // Are there fewer workers running as possible? Then fork a new one.
603                 if (!Config::get("system", "worker_dont_fork") && ($queues > ($active + 1)) && ($entries > 1)) {
604                         logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
605                         $args = array("include/poller.php", "no_cron");
606                         get_app()->proc_run($args);
607                 }
608         }
609
610         return $active >= $queues;
611 }
612
613 /**
614  * @brief Returns the number of active poller processes
615  *
616  * @return integer Number of active poller processes
617  */
618 function poller_active_workers() {
619         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
620
621         return $workers[0]["processes"];
622 }
623
624 /**
625  * @brief Check if we should pass some slow processes
626  *
627  * When the active processes of the highest priority are using more than 2/3
628  * of all processes, we let pass slower processes.
629  *
630  * @param string $highest_priority Returns the currently highest priority
631  * @return bool We let pass a slower process than $highest_priority
632  */
633 function poller_passing_slow(&$highest_priority) {
634
635         $highest_priority = 0;
636
637         $r = q("SELECT `priority`
638                 FROM `process`
639                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done`");
640
641         // No active processes at all? Fine
642         if (!dbm::is_result($r)) {
643                 return false;
644         }
645         $priorities = array();
646         foreach ($r AS $line) {
647                 $priorities[] = $line["priority"];
648         }
649         // Should not happen
650         if (count($priorities) == 0) {
651                 return false;
652         }
653         $highest_priority = min($priorities);
654
655         // The highest process is already the slowest one?
656         // Then we quit
657         if ($highest_priority == PRIORITY_NEGLIGIBLE) {
658                 return false;
659         }
660         $high = 0;
661         foreach ($priorities AS $priority) {
662                 if ($priority == $highest_priority) {
663                         ++$high;
664                 }
665         }
666         logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
667         $passing_slow = (($high/count($priorities)) > (2/3));
668
669         if ($passing_slow) {
670                 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
671         }
672         return $passing_slow;
673 }
674
675 /**
676  * @brief Find and claim the next worker process for us
677  *
678  * @param boolean $passing_slow Returns if we had passed low priority processes
679  * @return boolean Have we found something?
680  */
681 function find_worker_processes(&$passing_slow) {
682
683         $mypid = getmypid();
684
685         // Check if we should pass some low priority process
686         $highest_priority = 0;
687         $found = false;
688         $passing_slow = false;
689
690         // The higher the number of parallel workers, the more we prefetch to prevent concurring access
691         // We decrease the limit with the number of entries left in the queue
692         $worker_queues = Config::get("system", "worker_queues", 4);
693         $queue_length = Config::get('system', 'worker_fetch_limit', $worker_queues);
694         $lower_job_limit = $worker_queues * $queue_length * 2;
695         $jobs = poller_total_entries();
696
697         // Now do some magic
698         $exponent = 2;
699         $slope = $queue_length / pow($lower_job_limit, $exponent);
700         $limit = min($queue_length, ceil($slope * pow($jobs, $exponent)));
701
702         logger('Total: '.$jobs.' - Maximum: '.$queue_length.' - jobs per queue: '.$limit, LOGGER_DEBUG);
703
704         if (poller_passing_slow($highest_priority)) {
705                 // Are there waiting processes with a higher priority than the currently highest?
706                 $result = dba::select('workerqueue', array('id'), array("`executed` <= ? AND `priority` < ? AND NOT `done`", NULL_DATE, $highest_priority),
707                                 array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true));
708
709                 while ($id = dba::fetch($result)) {
710                         $ids[] = $id["id"];
711                 }
712                 dba::close($result);
713
714                 $found = (count($ids) > 0);
715
716                 if (!$found) {
717                         // Give slower processes some processing time
718                         $result = dba::select('workerqueue', array('id'), array("`executed` <= ? AND `priority` > ? AND NOT `done`", NULL_DATE, $highest_priority),
719                                         array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true));
720
721                         while ($id = dba::fetch($result)) {
722                                 $ids[] = $id["id"];
723                         }
724                         dba::close($result);
725
726                         $found = (count($ids) > 0);
727                         $passing_slow = $found;
728                 }
729         }
730
731         // If there is no result (or we shouldn't pass lower processes) we check without priority limit
732         if (!$found) {
733                 $result = dba::select('workerqueue', array('id'), array("`executed` <= ? AND NOT `done`", NULL_DATE),
734                                 array('limit' => $limit, 'order' => array('priority', 'created'), 'only_query' => true));
735
736                 while ($id = dba::fetch($result)) {
737                         $ids[] = $id["id"];
738                 }
739                 dba::close($result);
740
741                 $found = (count($ids) > 0);
742         }
743
744         if ($found) {
745                 $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
746                 array_unshift($ids, $condition);
747                 dba::update('workerqueue', array('executed' => datetime_convert(), 'pid' => $mypid), $ids);
748         }
749
750         return $found;
751 }
752
753 /**
754  * @brief Returns the next worker process
755  *
756  * @param boolean $passing_slow Returns if we had passed low priority processes
757  * @return string SQL statement
758  */
759 function poller_worker_process(&$passing_slow) {
760         global $poller_db_duration, $poller_lock_duration;
761
762         $stamp = (float)microtime(true);
763
764         // There can already be jobs for us in the queue.
765         $r = q("SELECT * FROM `workerqueue` WHERE `pid` = %d AND NOT `done`", intval(getmypid()));
766         if (dbm::is_result($r)) {
767                 $poller_db_duration += (microtime(true) - $stamp);
768                 return $r;
769         }
770
771         $stamp = (float)microtime(true);
772         if (!Lock::set('poller_worker_process')) {
773                 return false;
774         }
775         $poller_lock_duration = (microtime(true) - $stamp);
776
777         $stamp = (float)microtime(true);
778         $found = find_worker_processes($passing_slow);
779         $poller_db_duration += (microtime(true) - $stamp);
780
781         Lock::remove('poller_worker_process');
782
783         if ($found) {
784                 $r = q("SELECT * FROM `workerqueue` WHERE `pid` = %d AND NOT `done`", intval(getmypid()));
785         }
786         return $r;
787 }
788
789 /**
790  * @brief Removes a workerqueue entry from the current process
791  */
792 function poller_unclaim_process() {
793         $mypid = getmypid();
794
795         dba::update('workerqueue', array('executed' => NULL_DATE, 'pid' => 0), array('pid' => $mypid, 'done' => false));
796 }
797
798 /**
799  * @brief Call the front end worker
800  */
801 function call_worker() {
802         if (!Config::get("system", "frontend_worker")) {
803                 return;
804         }
805
806         $url = System::baseUrl()."/worker";
807         fetch_url($url, false, $redirects, 1);
808 }
809
810 /**
811  * @brief Call the front end worker if there aren't any active
812  */
813 function call_worker_if_idle() {
814         if (!Config::get("system", "frontend_worker")) {
815                 return;
816         }
817
818         // Do we have "proc_open"? Then we can fork the poller
819         if (function_exists("proc_open")) {
820                 // When was the last time that we called the worker?
821                 // Less than one minute? Then we quit
822                 if ((time() - Config::get("system", "worker_started")) < 60) {
823                         return;
824                 }
825
826                 set_config("system", "worker_started", time());
827
828                 // Do we have enough running workers? Then we quit here.
829                 if (poller_too_much_workers()) {
830                         // Cleaning dead processes
831                         poller_kill_stale_workers();
832                         get_app()->remove_inactive_processes();
833
834                         return;
835                 }
836
837                 poller_run_cron();
838
839                 logger('Call poller', LOGGER_DEBUG);
840
841                 $args = array("include/poller.php", "no_cron");
842                 get_app()->proc_run($args);
843                 return;
844         }
845
846         // We cannot execute background processes.
847         // We now run the processes from the frontend.
848         // This won't work with long running processes.
849         poller_run_cron();
850
851         clear_worker_processes();
852
853         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
854
855         if ($workers[0]["processes"] == 0) {
856                 call_worker();
857         }
858 }
859
860 /**
861  * @brief Removes long running worker processes
862  */
863 function clear_worker_processes() {
864         $timeout = Config::get("system", "frontend_worker_timeout", 10);
865
866         /// @todo We should clean up the corresponding workerqueue entries as well
867         q("DELETE FROM `process` WHERE `created` < '%s' AND `command` = 'worker.php'",
868                 dbesc(datetime_convert('UTC','UTC',"now - ".$timeout." minutes")));
869 }
870
871 /**
872  * @brief Runs the cron processes
873  */
874 function poller_run_cron() {
875         logger('Add cron entries', LOGGER_DEBUG);
876
877         // Check for spooled items
878         proc_run(PRIORITY_HIGH, "include/spool_post.php");
879
880         // Run the cron job that calls all other jobs
881         proc_run(PRIORITY_MEDIUM, "include/cron.php");
882
883         // Run the cronhooks job separately from cron for being able to use a different timing
884         proc_run(PRIORITY_MEDIUM, "include/cronhooks.php");
885
886         // Cleaning dead processes
887         poller_kill_stale_workers();
888 }
889
890 if (array_search(__file__,get_included_files())===0){
891         poller_run($_SERVER["argv"],$_SERVER["argc"]);
892
893         poller_unclaim_process();
894
895         get_app()->end_process();
896
897         killme();
898 }