]> git.mxchange.org Git - friendica.git/blob - src/Core/Worker.php
Rename App Methods
[friendica.git] / src / Core / Worker.php
1 <?php
2 /**
3  * @file src/Core/Worker.php
4  */
5 namespace Friendica\Core;
6
7 use Friendica\Database\DBA;
8 use Friendica\Model\Process;
9 use Friendica\Util\DateTimeFormat;
10 use Friendica\Util\Network;
11
12 require_once 'include/dba.php';
13
14 /**
15  * @file src/Core/Worker.php
16  *
17  * @brief Contains the class for the worker background job processing
18  */
19
20 /**
21  * @brief Worker methods
22  */
23 class Worker
24 {
25         private static $up_start;
26         private static $db_duration;
27         private static $last_update;
28         private static $lock_duration;
29
30         /**
31          * @brief Processes the tasks that are in the workerqueue table
32          *
33          * @param boolean $run_cron Should the cron processes be executed?
34          * @return void
35          */
36         public static function processQueue($run_cron = true)
37         {
38                 $a = get_app();
39
40                 self::$up_start = microtime(true);
41
42                 // At first check the maximum load. We shouldn't continue with a high load
43                 if ($a->isMaxLoadReached()) {
44                         logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG);
45                         return;
46                 }
47
48                 // We now start the process. This is done after the load check since this could increase the load.
49                 self::startProcess();
50
51                 // Kill stale processes every 5 minutes
52                 $last_cleanup = Config::get('system', 'worker_last_cleaned', 0);
53                 if (time() > ($last_cleanup + 300)) {
54                         Config::set('system', 'worker_last_cleaned', time());
55                         self::killStaleWorkers();
56                 }
57
58                 // Count active workers and compare them with a maximum value that depends on the load
59                 if (self::tooMuchWorkers()) {
60                         logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
61                         return;
62                 }
63
64                 // Do we have too few memory?
65                 if ($a->isMinMemoryReached()) {
66                         logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
67                         return;
68                 }
69
70                 // Possibly there are too much database connections
71                 if (self::maxConnectionsReached()) {
72                         logger('Pre check: maximum connections reached, quitting.', LOGGER_DEBUG);
73                         return;
74                 }
75
76                 // Possibly there are too much database processes that block the system
77                 if ($a->isMaxProcessesReached()) {
78                         logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG);
79                         return;
80                 }
81
82                 // Now we start additional cron processes if we should do so
83                 if ($run_cron) {
84                         self::runCron();
85                 }
86
87                 $starttime = time();
88
89                 // We fetch the next queue entry that is about to be executed
90                 while ($r = self::workerProcess($passing_slow)) {
91                         // When we are processing jobs with a lower priority, we don't refetch new jobs
92                         // Otherwise fast jobs could wait behind slow ones and could be blocked.
93                         $refetched = $passing_slow;
94
95                         foreach ($r as $entry) {
96                                 // Assure that the priority is an integer value
97                                 $entry['priority'] = (int)$entry['priority'];
98
99                                 // The work will be done
100                                 if (!self::execute($entry)) {
101                                         logger('Process execution failed, quitting.', LOGGER_DEBUG);
102                                         return;
103                                 }
104
105                                 // If possible we will fetch new jobs for this worker
106                                 if (!$refetched && Lock::acquire('worker_process', 0)) {
107                                         $stamp = (float)microtime(true);
108                                         $refetched = self::findWorkerProcesses($passing_slow);
109                                         self::$db_duration += (microtime(true) - $stamp);
110                                         Lock::release('worker_process');
111                                 }
112                         }
113
114                         // To avoid the quitting of multiple workers only one worker at a time will execute the check
115                         if (Lock::acquire('worker', 0)) {
116                                 $stamp = (float)microtime(true);
117                                 // Count active workers and compare them with a maximum value that depends on the load
118                                 if (self::tooMuchWorkers()) {
119                                         logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
120                                         Lock::release('worker');
121                                         return;
122                                 }
123
124                                 // Check free memory
125                                 if ($a->isMinMemoryReached()) {
126                                         logger('Memory limit reached, quitting.', LOGGER_DEBUG);
127                                         Lock::release('worker');
128                                         return;
129                                 }
130                                 Lock::release('worker');
131                                 self::$db_duration += (microtime(true) - $stamp);
132                         }
133
134                         // Quit the worker once every 5 minutes
135                         if (time() > ($starttime + 300)) {
136                                 logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
137                                 return;
138                         }
139                 }
140
141                 // Cleaning up. Possibly not needed, but it doesn't harm anything.
142                 if (Config::get('system', 'worker_daemon_mode', false)) {
143                         self::IPCSetJobState(false);
144                 }
145                 logger("Couldn't select a workerqueue entry, quitting process " . getmypid() . ".", LOGGER_DEBUG);
146         }
147
148         /**
149          * @brief Returns the number of non executed entries in the worker queue
150          *
151          * @return integer Number of non executed entries in the worker queue
152          */
153         private static function totalEntries()
154         {
155                 return DBA::count('workerqueue', ["`executed` <= ? AND NOT `done`", NULL_DATE]);
156         }
157
158         /**
159          * @brief Returns the highest priority in the worker queue that isn't executed
160          *
161          * @return integer Number of active worker processes
162          */
163         private static function highestPriority()
164         {
165                 $condition = ["`executed` <= ? AND NOT `done`", NULL_DATE];
166                 $workerqueue = DBA::selectFirst('workerqueue', ['priority'], $condition, ['order' => ['priority']]);
167                 if (DBA::isResult($workerqueue)) {
168                         return $workerqueue["priority"];
169                 } else {
170                         return 0;
171                 }
172         }
173
174         /**
175          * @brief Returns if a process with the given priority is running
176          *
177          * @param integer $priority The priority that should be checked
178          *
179          * @return integer Is there a process running with that priority?
180          */
181         private static function processWithPriorityActive($priority)
182         {
183                 $condition = ["`priority` <= ? AND `executed` > ? AND NOT `done`", $priority, NULL_DATE];
184                 return DBA::exists('workerqueue', $condition);
185         }
186
187         /**
188          * @brief Execute a worker entry
189          *
190          * @param array $queue Workerqueue entry
191          *
192          * @return boolean "true" if further processing should be stopped
193          */
194         public static function execute($queue)
195         {
196                 $a = get_app();
197
198                 $mypid = getmypid();
199
200                 // Quit when in maintenance
201                 if (Config::get('system', 'maintenance', false, true)) {
202                         logger("Maintenance mode - quit process ".$mypid, LOGGER_DEBUG);
203                         return false;
204                 }
205
206                 // Constantly check the number of parallel database processes
207                 if ($a->isMaxProcessesReached()) {
208                         logger("Max processes reached for process ".$mypid, LOGGER_DEBUG);
209                         return false;
210                 }
211
212                 // Constantly check the number of available database connections to let the frontend be accessible at any time
213                 if (self::maxConnectionsReached()) {
214                         logger("Max connection reached for process ".$mypid, LOGGER_DEBUG);
215                         return false;
216                 }
217
218                 $argv = json_decode($queue["parameter"], true);
219
220                 // Check for existance and validity of the include file
221                 $include = $argv[0];
222
223                 if (method_exists(sprintf('Friendica\Worker\%s', $include), 'execute')) {
224                         // We constantly update the "executed" date every minute to avoid being killed too soon
225                         if (!isset(self::$last_update)) {
226                                 self::$last_update = strtotime($queue["executed"]);
227                         }
228
229                         $age = (time() - self::$last_update) / 60;
230                         self::$last_update = time();
231
232                         if ($age > 1) {
233                                 $stamp = (float)microtime(true);
234                                 DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
235                                 self::$db_duration += (microtime(true) - $stamp);
236                         }
237
238                         array_shift($argv);
239
240                         self::execFunction($queue, $include, $argv, true);
241
242                         $stamp = (float)microtime(true);
243                         if (DBA::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
244                                 Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
245                         }
246                         self::$db_duration = (microtime(true) - $stamp);
247
248                         return true;
249                 }
250
251                 // The script could be provided as full path or only with the function name
252                 if ($include == basename($include)) {
253                         $include = "include/".$include.".php";
254                 }
255
256                 if (!validate_include($include)) {
257                         logger("Include file ".$argv[0]." is not valid!");
258                         DBA::delete('workerqueue', ['id' => $queue["id"]]);
259                         return true;
260                 }
261
262                 require_once $include;
263
264                 $funcname = str_replace(".php", "", basename($argv[0]))."_run";
265
266                 if (function_exists($funcname)) {
267                         // We constantly update the "executed" date every minute to avoid being killed too soon
268                         if (!isset(self::$last_update)) {
269                                 self::$last_update = strtotime($queue["executed"]);
270                         }
271
272                         $age = (time() - self::$last_update) / 60;
273                         self::$last_update = time();
274
275                         if ($age > 1) {
276                                 $stamp = (float)microtime(true);
277                                 DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
278                                 self::$db_duration += (microtime(true) - $stamp);
279                         }
280
281                         self::execFunction($queue, $funcname, $argv, false);
282
283                         $stamp = (float)microtime(true);
284                         if (DBA::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
285                                 Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
286                         }
287                         self::$db_duration = (microtime(true) - $stamp);
288                 } else {
289                         logger("Function ".$funcname." does not exist");
290                         DBA::delete('workerqueue', ['id' => $queue["id"]]);
291                 }
292
293                 return true;
294         }
295
296         /**
297          * @brief Execute a function from the queue
298          *
299          * @param array   $queue       Workerqueue entry
300          * @param string  $funcname    name of the function
301          * @param array   $argv        Array of values to be passed to the function
302          * @param boolean $method_call boolean
303          * @return void
304          */
305         private static function execFunction($queue, $funcname, $argv, $method_call)
306         {
307                 $a = get_app();
308
309                 $mypid = getmypid();
310
311                 $argc = count($argv);
312
313                 $new_process_id = System::processID("wrk");
314
315                 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
316
317                 $stamp = (float)microtime(true);
318
319                 // We use the callstack here to analyze the performance of executed worker entries.
320                 // For this reason the variables have to be initialized.
321                 if (Config::get("system", "profiler")) {
322                         $a->performance["start"] = microtime(true);
323                         $a->performance["database"] = 0;
324                         $a->performance["database_write"] = 0;
325                         $a->performance["cache"] = 0;
326                         $a->performance["cache_write"] = 0;
327                         $a->performance["network"] = 0;
328                         $a->performance["file"] = 0;
329                         $a->performance["rendering"] = 0;
330                         $a->performance["parser"] = 0;
331                         $a->performance["marktime"] = 0;
332                         $a->performance["markstart"] = microtime(true);
333                         $a->callstack = [];
334                 }
335
336                 // For better logging create a new process id for every worker call
337                 // But preserve the old one for the worker
338                 $old_process_id = $a->process_id;
339                 $a->process_id = $new_process_id;
340                 $a->queue = $queue;
341
342                 $up_duration = number_format(microtime(true) - self::$up_start, 3);
343
344                 // Reset global data to avoid interferences
345                 unset($_SESSION);
346
347                 if ($method_call) {
348                         call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv);
349                 } else {
350                         $funcname($argv, $argc);
351                 }
352
353                 $a->process_id = $old_process_id;
354                 unset($a->queue);
355
356                 $duration = (microtime(true) - $stamp);
357
358                 self::$up_start = microtime(true);
359
360                 /* With these values we can analyze how effective the worker is.
361                  * The database and rest time should be low since this is the unproductive time.
362                  * The execution time is the productive time.
363                  * By changing parameters like the maximum number of workers we can check the effectivness.
364                 */
365                 logger(
366                         'DB: '.number_format(self::$db_duration, 2).
367                         ' - Lock: '.number_format(self::$lock_duration, 2).
368                         ' - Rest: '.number_format($up_duration - self::$db_duration - self::$lock_duration, 2).
369                         ' - Execution: '.number_format($duration, 2),
370                         LOGGER_DEBUG
371                 );
372
373                 self::$lock_duration = 0;
374
375                 if ($duration > 3600) {
376                         logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
377                 } elseif ($duration > 600) {
378                         logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
379                 } elseif ($duration > 300) {
380                         logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
381                 } elseif ($duration > 120) {
382                         logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
383                 }
384
385                 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds. Process PID: ".$new_process_id);
386
387                 // Write down the performance values into the log
388                 if (Config::get("system", "profiler")) {
389                         $duration = microtime(true)-$a->performance["start"];
390
391                         $o = '';
392                         if (Config::get("rendertime", "callstack")) {
393                                 if (isset($a->callstack["database"])) {
394                                         $o .= "\nDatabase Read:\n";
395                                         foreach ($a->callstack["database"] as $func => $time) {
396                                                 $time = round($time, 3);
397                                                 if ($time > 0) {
398                                                         $o .= $func.": ".$time."\n";
399                                                 }
400                                         }
401                                 }
402                                 if (isset($a->callstack["database_write"])) {
403                                         $o .= "\nDatabase Write:\n";
404                                         foreach ($a->callstack["database_write"] as $func => $time) {
405                                                 $time = round($time, 3);
406                                                 if ($time > 0) {
407                                                         $o .= $func.": ".$time."\n";
408                                                 }
409                                         }
410                                 }
411                                 if (isset($a->callstack["dache"])) {
412                                         $o .= "\nCache Read:\n";
413                                         foreach ($a->callstack["dache"] as $func => $time) {
414                                                 $time = round($time, 3);
415                                                 if ($time > 0) {
416                                                         $o .= $func.": ".$time."\n";
417                                                 }
418                                         }
419                                 }
420                                 if (isset($a->callstack["dache_write"])) {
421                                         $o .= "\nCache Write:\n";
422                                         foreach ($a->callstack["dache_write"] as $func => $time) {
423                                                 $time = round($time, 3);
424                                                 if ($time > 0) {
425                                                         $o .= $func.": ".$time."\n";
426                                                 }
427                                         }
428                                 }
429                                 if (isset($a->callstack["network"])) {
430                                         $o .= "\nNetwork:\n";
431                                         foreach ($a->callstack["network"] as $func => $time) {
432                                                 $time = round($time, 3);
433                                                 if ($time > 0) {
434                                                         $o .= $func.": ".$time."\n";
435                                                 }
436                                         }
437                                 }
438                         }
439
440                         logger(
441                                 "ID ".$queue["id"].": ".$funcname.": ".sprintf(
442                                         "DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
443                                         number_format($a->performance["database"] - $a->performance["database_write"], 2),
444                                         number_format($a->performance["database_write"], 2),
445                                         number_format($a->performance["cache"], 2),
446                                         number_format($a->performance["cache_write"], 2),
447                                         number_format($a->performance["network"], 2),
448                                         number_format($a->performance["file"], 2),
449                                         number_format($duration - ($a->performance["database"]
450                                                 + $a->performance["cache"] + $a->performance["cache_write"]
451                                                 + $a->performance["network"] + $a->performance["file"]), 2),
452                                         number_format($duration, 2)
453                                 ),
454                                 LOGGER_DEBUG
455                         );
456                 }
457
458                 $cooldown = Config::get("system", "worker_cooldown", 0);
459
460                 if ($cooldown > 0) {
461                         logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
462                         sleep($cooldown);
463                 }
464         }
465
466         /**
467          * @brief Checks if the number of database connections has reached a critical limit.
468          *
469          * @return bool Are more than 3/4 of the maximum connections used?
470          */
471         private static function maxConnectionsReached()
472         {
473                 // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
474                 $max = Config::get("system", "max_connections");
475
476                 // Fetch the percentage level where the worker will get active
477                 $maxlevel = Config::get("system", "max_connections_level", 75);
478
479                 if ($max == 0) {
480                         // the maximum number of possible user connections can be a system variable
481                         $r = DBA::fetchFirst("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
482                         if (DBA::isResult($r)) {
483                                 $max = $r["Value"];
484                         }
485                         // Or it can be granted. This overrides the system variable
486                         $r = DBA::p('SHOW GRANTS');
487                         while ($grants = DBA::fetch($r)) {
488                                 $grant = array_pop($grants);
489                                 if (stristr($grant, "GRANT USAGE ON")) {
490                                         if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match)) {
491                                                 $max = $match[1];
492                                         }
493                                 }
494                         }
495                         DBA::close($r);
496                 }
497
498                 // If $max is set we will use the processlist to determine the current number of connections
499                 // The processlist only shows entries of the current user
500                 if ($max != 0) {
501                         $r = DBA::p('SHOW PROCESSLIST');
502                         $used = DBA::numRows($r);
503                         DBA::close($r);
504
505                         logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
506
507                         $level = ($used / $max) * 100;
508
509                         if ($level >= $maxlevel) {
510                                 logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
511                                 return true;
512                         }
513                 }
514
515                 // We will now check for the system values.
516                 // This limit could be reached although the user limits are fine.
517                 $r = DBA::fetchFirst("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
518                 if (!DBA::isResult($r)) {
519                         return false;
520                 }
521                 $max = intval($r["Value"]);
522                 if ($max == 0) {
523                         return false;
524                 }
525                 $r = DBA::fetchFirst("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
526                 if (!DBA::isResult($r)) {
527                         return false;
528                 }
529                 $used = intval($r["Value"]);
530                 if ($used == 0) {
531                         return false;
532                 }
533                 logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
534
535                 $level = $used / $max * 100;
536
537                 if ($level < $maxlevel) {
538                         return false;
539                 }
540                 logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
541                 return true;
542         }
543
544         /**
545          * @brief fix the queue entry if the worker process died
546          * @return void
547          */
548         private static function killStaleWorkers()
549         {
550                 $entries = DBA::select(
551                         'workerqueue',
552                         ['id', 'pid', 'executed', 'priority', 'parameter'],
553                         ['`executed` > ? AND NOT `done` AND `pid` != 0', NULL_DATE],
554                         ['order' => ['priority', 'created']]
555                 );
556
557                 while ($entry = DBA::fetch($entries)) {
558                         if (!posix_kill($entry["pid"], 0)) {
559                                 DBA::update(
560                                         'workerqueue',
561                                         ['executed' => NULL_DATE, 'pid' => 0],
562                                         ['id' => $entry["id"]]
563                                 );
564                         } else {
565                                 // Kill long running processes
566                                 // Check if the priority is in a valid range
567                                 if (!in_array($entry["priority"], [PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE])) {
568                                         $entry["priority"] = PRIORITY_MEDIUM;
569                                 }
570
571                                 // Define the maximum durations
572                                 $max_duration_defaults = [PRIORITY_CRITICAL => 720, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 720];
573                                 $max_duration = $max_duration_defaults[$entry["priority"]];
574
575                                 $argv = json_decode($entry["parameter"], true);
576                                 $argv[0] = basename($argv[0]);
577
578                                 // How long is the process already running?
579                                 $duration = (time() - strtotime($entry["executed"])) / 60;
580                                 if ($duration > $max_duration) {
581                                         logger("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") took more than ".$max_duration." minutes. It will be killed now.");
582                                         posix_kill($entry["pid"], SIGTERM);
583
584                                         // We killed the stale process.
585                                         // To avoid a blocking situation we reschedule the process at the beginning of the queue.
586                                         // Additionally we are lowering the priority. (But not PRIORITY_CRITICAL)
587                                         $new_priority = $entry["priority"];
588                                         if ($entry["priority"] == PRIORITY_HIGH) {
589                                                 $new_priority = PRIORITY_MEDIUM;
590                                         } elseif ($entry["priority"] == PRIORITY_MEDIUM) {
591                                                 $new_priority = PRIORITY_LOW;
592                                         } elseif ($entry["priority"] != PRIORITY_CRITICAL) {
593                                                 $new_priority = PRIORITY_NEGLIGIBLE;
594                                         }
595                                         DBA::update(
596                                                 'workerqueue',
597                                                 ['executed' => NULL_DATE, 'created' => DateTimeFormat::utcNow(), 'priority' => $new_priority, 'pid' => 0],
598                                                 ['id' => $entry["id"]]
599                                         );
600                                 } else {
601                                         logger("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
602                                 }
603                         }
604                 }
605         }
606
607         /**
608          * @brief Checks if the number of active workers exceeds the given limits
609          *
610          * @return bool Are there too much workers running?
611          */
612         public static function tooMuchWorkers()
613         {
614                 $queues = Config::get("system", "worker_queues", 4);
615
616                 $maxqueues = $queues;
617
618                 $active = self::activeWorkers();
619
620                 // Decrease the number of workers at higher load
621                 $load = current_load();
622                 if ($load) {
623                         $maxsysload = intval(Config::get("system", "maxloadavg", 50));
624
625                         /* Default exponent 3 causes queues to rapidly decrease as load increases.
626                          * If you have 20 max queues at idle, then you get only 5 queues at 37.1% of $maxsysload.
627                          * For some environments, this rapid decrease is not needed.
628                          * With exponent 1, you could have 20 max queues at idle and 13 at 37% of $maxsysload.
629                          */
630                         $exponent = intval(Config::get('system', 'worker_load_exponent', 3));
631                         $slope = pow(max(0, $maxsysload - $load) / $maxsysload, $exponent);
632                         $queues = intval(ceil($slope * $maxqueues));
633
634                         $processlist = '';
635
636                         if (Config::get('system', 'worker_debug')) {
637                                 // Create a list of queue entries grouped by their priority
638                                 $listitem = [];
639
640                                 // Adding all processes with no workerqueue entry
641                                 $processes = DBA::p(
642                                         "SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS
643                                                         (SELECT id FROM `workerqueue`
644                                                         WHERE `workerqueue`.`pid` = `process`.`pid` AND NOT `done` AND `pid` != ?)",
645                                         getmypid()
646                                 );
647
648                                 if ($process = DBA::fetch($processes)) {
649                                         $listitem[0] = "0:".$process["running"];
650                                 }
651                                 DBA::close($processes);
652
653                                 // Now adding all processes with workerqueue entries
654                                 $entries = DBA::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` GROUP BY `priority`");
655                                 while ($entry = DBA::fetch($entries)) {
656                                         $processes = DBA::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done` WHERE `priority` = ?", $entry["priority"]);
657                                         if ($process = DBA::fetch($processes)) {
658                                                 $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
659                                         }
660                                         DBA::close($processes);
661                                 }
662                                 DBA::close($entries);
663
664                                 $intervals = [1, 10, 60];
665                                 $jobs_per_minute = [];
666                                 foreach ($intervals as $interval) {
667                                         $jobs = DBA::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE");
668                                         if ($job = DBA::fetch($jobs)) {
669                                                 $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
670                                         }
671                                         DBA::close($jobs);
672                                 }
673                                 $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')';
674                         }
675
676                         $entries = self::totalEntries();
677
678                         if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
679                                 $top_priority = self::highestPriority();
680                                 $high_running = self::processWithPriorityActive($top_priority);
681
682                                 if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
683                                         logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
684                                         $queues = $active + 1;
685                                 }
686                         }
687
688                         logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries.$processlist." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
689
690                         // Are there fewer workers running as possible? Then fork a new one.
691                         if (!Config::get("system", "worker_dont_fork", false) && ($queues > ($active + 1)) && ($entries > 1)) {
692                                 logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
693                                 if (Config::get('system', 'worker_daemon_mode', false)) {
694                                         self::IPCSetJobState(true);
695                                 } else {
696                                         self::spawnWorker();
697                                 }
698                         }
699                 }
700
701                 // if there are too much worker, we don't spawn a new one.
702                 if (Config::get('system', 'worker_daemon_mode', false) && ($active > $queues)) {
703                         self::IPCSetJobState(false);
704                 }
705
706                 return $active > $queues;
707         }
708
709         /**
710          * @brief Returns the number of active worker processes
711          *
712          * @return integer Number of active worker processes
713          */
714         private static function activeWorkers()
715         {
716                 return DBA::count('process', ['command' => 'Worker.php']);
717         }
718
719         /**
720          * @brief Check if we should pass some slow processes
721          *
722          * When the active processes of the highest priority are using more than 2/3
723          * of all processes, we let pass slower processes.
724          *
725          * @param string $highest_priority Returns the currently highest priority
726          * @return bool We let pass a slower process than $highest_priority
727          */
728         private static function passingSlow(&$highest_priority)
729         {
730                 $highest_priority = 0;
731
732                 $r = DBA::p(
733                         "SELECT `priority`
734                                 FROM `process`
735                                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done`"
736                 );
737
738                 // No active processes at all? Fine
739                 if (!DBA::isResult($r)) {
740                         return false;
741                 }
742                 $priorities = [];
743                 while ($line = DBA::fetch($r)) {
744                         $priorities[] = $line["priority"];
745                 }
746                 DBA::close($r);
747
748                 // Should not happen
749                 if (count($priorities) == 0) {
750                         return false;
751                 }
752                 $highest_priority = min($priorities);
753
754                 // The highest process is already the slowest one?
755                 // Then we quit
756                 if ($highest_priority == PRIORITY_NEGLIGIBLE) {
757                         return false;
758                 }
759                 $high = 0;
760                 foreach ($priorities as $priority) {
761                         if ($priority == $highest_priority) {
762                                 ++$high;
763                         }
764                 }
765                 logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
766                 $passing_slow = (($high/count($priorities)) > (2/3));
767
768                 if ($passing_slow) {
769                         logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
770                 }
771                 return $passing_slow;
772         }
773
774         /**
775          * @brief Find and claim the next worker process for us
776          *
777          * @param boolean $passing_slow Returns if we had passed low priority processes
778          * @return boolean Have we found something?
779          */
780         private static function findWorkerProcesses(&$passing_slow)
781         {
782                 $mypid = getmypid();
783
784                 // Check if we should pass some low priority process
785                 $highest_priority = 0;
786                 $found = false;
787                 $passing_slow = false;
788
789                 // The higher the number of parallel workers, the more we prefetch to prevent concurring access
790                 // We decrease the limit with the number of entries left in the queue
791                 $worker_queues = Config::get("system", "worker_queues", 4);
792                 $queue_length = Config::get('system', 'worker_fetch_limit', 1);
793                 $lower_job_limit = $worker_queues * $queue_length * 2;
794                 $jobs = self::totalEntries();
795
796                 // Now do some magic
797                 $exponent = 2;
798                 $slope = $queue_length / pow($lower_job_limit, $exponent);
799                 $limit = min($queue_length, ceil($slope * pow($jobs, $exponent)));
800
801                 logger('Total: '.$jobs.' - Maximum: '.$queue_length.' - jobs per queue: '.$limit, LOGGER_DEBUG);
802                 $ids = [];
803                 if (self::passingSlow($highest_priority)) {
804                         // Are there waiting processes with a higher priority than the currently highest?
805                         $result = DBA::select(
806                                 'workerqueue',
807                                 ['id'],
808                                 ["`executed` <= ? AND `priority` < ? AND NOT `done`", NULL_DATE, $highest_priority],
809                                 ['limit' => $limit, 'order' => ['priority', 'created']]
810                         );
811
812                         while ($id = DBA::fetch($result)) {
813                                 $ids[] = $id["id"];
814                         }
815                         DBA::close($result);
816
817                         $found = (count($ids) > 0);
818
819                         if (!$found) {
820                                 // Give slower processes some processing time
821                                 $result = DBA::select(
822                                         'workerqueue',
823                                         ['id'],
824                                         ["`executed` <= ? AND `priority` > ? AND NOT `done`", NULL_DATE, $highest_priority],
825                                         ['limit' => $limit, 'order' => ['priority', 'created']]
826                                 );
827
828                                 while ($id = DBA::fetch($result)) {
829                                         $ids[] = $id["id"];
830                                 }
831                                 DBA::close($result);
832
833                                 $found = (count($ids) > 0);
834                                 $passing_slow = $found;
835                         }
836                 }
837
838                 // If there is no result (or we shouldn't pass lower processes) we check without priority limit
839                 if (!$found) {
840                         $result = DBA::select(
841                                 'workerqueue',
842                                 ['id'],
843                                 ["`executed` <= ? AND NOT `done`", NULL_DATE],
844                                 ['limit' => $limit, 'order' => ['priority', 'created']]
845                         );
846
847                         while ($id = DBA::fetch($result)) {
848                                 $ids[] = $id["id"];
849                         }
850                         DBA::close($result);
851
852                         $found = (count($ids) > 0);
853                 }
854
855                 if ($found) {
856                         $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
857                         array_unshift($ids, $condition);
858                         DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $mypid], $ids);
859                 }
860
861                 return $found;
862         }
863
864         /**
865          * @brief Returns the next worker process
866          *
867          * @param boolean $passing_slow Returns if we had passed low priority processes
868          * @return string SQL statement
869          */
870         public static function workerProcess(&$passing_slow)
871         {
872                 $stamp = (float)microtime(true);
873
874                 // There can already be jobs for us in the queue.
875                 $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
876                 if (DBA::isResult($r)) {
877                         self::$db_duration += (microtime(true) - $stamp);
878                         return DBA::toArray($r);
879                 }
880                 DBA::close($r);
881
882                 $stamp = (float)microtime(true);
883                 if (!Lock::acquire('worker_process')) {
884                         return false;
885                 }
886                 self::$lock_duration = (microtime(true) - $stamp);
887
888                 $stamp = (float)microtime(true);
889                 $found = self::findWorkerProcesses($passing_slow);
890                 self::$db_duration += (microtime(true) - $stamp);
891
892                 Lock::release('worker_process');
893
894                 if ($found) {
895                         $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
896                         return DBA::toArray($r);
897                 }
898                 return false;
899         }
900
901         /**
902          * @brief Removes a workerqueue entry from the current process
903          * @return void
904          */
905         public static function unclaimProcess()
906         {
907                 $mypid = getmypid();
908
909                 DBA::update('workerqueue', ['executed' => NULL_DATE, 'pid' => 0], ['pid' => $mypid, 'done' => false]);
910         }
911
912         /**
913          * @brief Call the front end worker
914          * @return void
915          */
916         public static function callWorker()
917         {
918                 if (!Config::get("system", "frontend_worker")) {
919                         return;
920                 }
921
922                 $url = System::baseUrl()."/worker";
923                 Network::fetchUrl($url, false, $redirects, 1);
924         }
925
926         /**
927          * @brief Call the front end worker if there aren't any active
928          * @return void
929          */
930         public static function executeIfIdle()
931         {
932                 if (!Config::get("system", "frontend_worker")) {
933                         return;
934                 }
935
936                 // Do we have "proc_open"? Then we can fork the worker
937                 if (function_exists("proc_open")) {
938                         // When was the last time that we called the worker?
939                         // Less than one minute? Then we quit
940                         if ((time() - Config::get("system", "worker_started")) < 60) {
941                                 return;
942                         }
943
944                         Config::set("system", "worker_started", time());
945
946                         // Do we have enough running workers? Then we quit here.
947                         if (self::tooMuchWorkers()) {
948                                 // Cleaning dead processes
949                                 self::killStaleWorkers();
950                                 Process::deleteInactive();
951
952                                 return;
953                         }
954
955                         self::runCron();
956
957                         logger('Call worker', LOGGER_DEBUG);
958                         self::spawnWorker();
959                         return;
960                 }
961
962                 // We cannot execute background processes.
963                 // We now run the processes from the frontend.
964                 // This won't work with long running processes.
965                 self::runCron();
966
967                 self::clearProcesses();
968
969                 $workers = self::activeWorkers();
970
971                 if ($workers == 0) {
972                         self::callWorker();
973                 }
974         }
975
976         /**
977          * @brief Removes long running worker processes
978          * @return void
979          */
980         public static function clearProcesses()
981         {
982                 $timeout = Config::get("system", "frontend_worker_timeout", 10);
983
984                 /// @todo We should clean up the corresponding workerqueue entries as well
985                 $condition = ["`created` < ? AND `command` = 'worker.php'",
986                                 DateTimeFormat::utc("now - ".$timeout." minutes")];
987                 DBA::delete('process', $condition);
988         }
989
990         /**
991          * @brief Runs the cron processes
992          * @return void
993          */
994         private static function runCron()
995         {
996                 logger('Add cron entries', LOGGER_DEBUG);
997
998                 // Check for spooled items
999                 self::add(PRIORITY_HIGH, "SpoolPost");
1000
1001                 // Run the cron job that calls all other jobs
1002                 self::add(PRIORITY_MEDIUM, "Cron");
1003
1004                 // Cleaning dead processes
1005                 self::killStaleWorkers();
1006         }
1007
1008         /**
1009          * @brief Spawns a new worker
1010          * @return void
1011          */
1012         public static function spawnWorker($do_cron = false)
1013         {
1014                 $command = 'bin/worker.php';
1015
1016                 $args = ['no_cron' => !$do_cron];
1017
1018                 get_app()->proc_run($command, $args);
1019
1020                 // after spawning we have to remove the flag.
1021                 if (Config::get('system', 'worker_daemon_mode', false)) {
1022                         self::IPCSetJobState(false);
1023                 }
1024         }
1025
1026         /**
1027          * @brief Adds tasks to the worker queue
1028          *
1029          * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
1030          *
1031          * next args are passed as $cmd command line
1032          * or: Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
1033          * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
1034          *
1035          * @note $cmd and string args are surrounded with ""
1036          *
1037          * @hooks 'proc_run'
1038          *      array $arr
1039          *
1040          * @return boolean "false" if proc_run couldn't be executed
1041          */
1042         public static function add($cmd)
1043         {
1044                 $args = func_get_args();
1045
1046                 if (!count($args)) {
1047                         return false;
1048                 }
1049
1050                 $arr = ['args' => $args, 'run_cmd' => true];
1051
1052                 Addon::callHooks("proc_run", $arr);
1053                 if (!$arr['run_cmd'] || !count($args)) {
1054                         return true;
1055                 }
1056
1057                 $priority = PRIORITY_MEDIUM;
1058                 $dont_fork = Config::get("system", "worker_dont_fork", false);
1059                 $created = DateTimeFormat::utcNow();
1060
1061                 $run_parameter = array_shift($args);
1062
1063                 if (is_int($run_parameter)) {
1064                         $priority = $run_parameter;
1065                 } elseif (is_array($run_parameter)) {
1066                         if (isset($run_parameter['priority'])) {
1067                                 $priority = $run_parameter['priority'];
1068                         }
1069                         if (isset($run_parameter['created'])) {
1070                                 $created = $run_parameter['created'];
1071                         }
1072                         if (isset($run_parameter['dont_fork'])) {
1073                                 $dont_fork = $run_parameter['dont_fork'];
1074                         }
1075                 }
1076
1077                 $parameters = json_encode($args);
1078                 $found = DBA::exists('workerqueue', ['parameter' => $parameters, 'done' => false]);
1079
1080                 // Quit if there was a database error - a precaution for the update process to 3.5.3
1081                 if (DBA::errorNo() != 0) {
1082                         return false;
1083                 }
1084
1085                 if (!$found) {
1086                         DBA::insert('workerqueue', ['parameter' => $parameters, 'created' => $created, 'priority' => $priority]);
1087                 }
1088
1089                 // Should we quit and wait for the worker to be called as a cronjob?
1090                 if ($dont_fork) {
1091                         return true;
1092                 }
1093
1094                 // If there is a lock then we don't have to check for too much worker
1095                 if (!Lock::acquire('worker', 0)) {
1096                         return true;
1097                 }
1098
1099                 // If there are already enough workers running, don't fork another one
1100                 $quit = self::tooMuchWorkers();
1101                 Lock::release('worker');
1102
1103                 if ($quit) {
1104                         return true;
1105                 }
1106
1107                 // We tell the daemon that a new job entry exists
1108                 if (Config::get('system', 'worker_daemon_mode', false)) {
1109                         // We don't have to set the IPC flag - this is done in "tooMuchWorkers"
1110                         return true;
1111                 }
1112
1113                 // Now call the worker to execute the jobs that we just added to the queue
1114                 self::spawnWorker();
1115
1116                 return true;
1117         }
1118
1119         /**
1120          * Log active processes into the "process" table
1121          *
1122          * @brief Log active processes into the "process" table
1123          */
1124         public static function startProcess()
1125         {
1126                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
1127
1128                 $command = basename($trace[0]['file']);
1129
1130                 Process::deleteInactive();
1131
1132                 Process::insert($command);
1133         }
1134
1135         /**
1136          * Remove the active process from the "process" table
1137          *
1138          * @brief Remove the active process from the "process" table
1139          * @return bool
1140          */
1141         public static function endProcess()
1142         {
1143                 return Process::deleteByPid();
1144         }
1145
1146         /**
1147          * Set the flag if some job is waiting
1148          *
1149          * @brief Set the flag if some job is waiting
1150          * @param boolean $jobs Is there a waiting job?
1151          */
1152         public static function IPCSetJobState($jobs)
1153         {
1154                 DBA::update('worker-ipc', ['jobs' => $jobs], ['key' => 1], true);
1155         }
1156
1157         /**
1158          * Checks if some worker job waits to be executed
1159          *
1160          * @brief Checks if some worker job waits to be executed
1161          * @return bool
1162          */
1163         public static function IPCJobsExists()
1164         {
1165                 $row = DBA::selectFirst('worker-ipc', ['jobs'], ['key' => 1]);
1166
1167                 // When we don't have a row, no job is running
1168                 if (!DBA::isResult($row)) {
1169                         return false;
1170                 }
1171
1172                 return (bool)$row['jobs'];
1173         }
1174 }