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