]> git.mxchange.org Git - friendica.git/blob - src/Core/Worker.php
Fix PHPDoc comments project-wide
[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_debug')) {
661                                 // Create a list of queue entries grouped by their priority
662                                 $listitem = [];
663
664                                 // Adding all processes with no workerqueue entry
665                                 $processes = DBA::p(
666                                         "SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS
667                                                         (SELECT id FROM `workerqueue`
668                                                         WHERE `workerqueue`.`pid` = `process`.`pid` AND NOT `done` AND `pid` != ?)",
669                                         getmypid()
670                                 );
671
672                                 if ($process = DBA::fetch($processes)) {
673                                         $listitem[0] = "0:".$process["running"];
674                                 }
675                                 DBA::close($processes);
676
677                                 // Now adding all processes with workerqueue entries
678                                 $entries = DBA::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` AND `next_try` < ? GROUP BY `priority`", DateTimeFormat::utcNow());
679                                 while ($entry = DBA::fetch($entries)) {
680                                         $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` = ?",
681                                                 DateTimeFormat::utcNow(), $entry["priority"]);
682                                         if ($process = DBA::fetch($processes)) {
683                                                 $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
684                                         }
685                                         DBA::close($processes);
686                                 }
687                                 DBA::close($entries);
688
689                                 $intervals = [1, 10, 60];
690                                 $jobs_per_minute = [];
691                                 foreach ($intervals as $interval) {
692                                         $jobs = DBA::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE");
693                                         if ($job = DBA::fetch($jobs)) {
694                                                 $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
695                                         }
696                                         DBA::close($jobs);
697                                 }
698                                 $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')';
699                         }
700
701                         $entries = self::totalEntries();
702                         $deferred = self::deferredEntries();
703
704                         if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
705                                 $top_priority = self::highestPriority();
706                                 $high_running = self::processWithPriorityActive($top_priority);
707
708                                 if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
709                                         Logger::log("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", Logger::DEBUG);
710                                         $queues = $active + 1;
711                                 }
712                         }
713
714                         Logger::log("Load: " . $load ."/" . $maxsysload . " - processes: " . $deferred . "/" . $active . "/" . $entries . $processlist . " - maximum: " . $queues . "/" . $maxqueues, Logger::DEBUG);
715
716                         // Are there fewer workers running as possible? Then fork a new one.
717                         if (!Config::get("system", "worker_dont_fork", false) && ($queues > ($active + 1)) && ($entries > 1)) {
718                                 Logger::log("Active workers: ".$active."/".$queues." Fork a new worker.", Logger::DEBUG);
719                                 if (Config::get('system', 'worker_daemon_mode', false)) {
720                                         self::IPCSetJobState(true);
721                                 } else {
722                                         self::spawnWorker();
723                                 }
724                         }
725                 }
726
727                 // if there are too much worker, we don't spawn a new one.
728                 if (Config::get('system', 'worker_daemon_mode', false) && ($active > $queues)) {
729                         self::IPCSetJobState(false);
730                 }
731
732                 return $active > $queues;
733         }
734
735         /**
736          * @brief Returns the number of active worker processes
737          *
738          * @return integer Number of active worker processes
739          * @throws \Exception
740          */
741         private static function activeWorkers()
742         {
743                 return DBA::count('process', ['command' => 'Worker.php']);
744         }
745
746         /**
747          * @brief Check if we should pass some slow processes
748          *
749          * When the active processes of the highest priority are using more than 2/3
750          * of all processes, we let pass slower processes.
751          *
752          * @param string $highest_priority Returns the currently highest priority
753          * @return bool We let pass a slower process than $highest_priority
754          * @throws \Exception
755          */
756         private static function passingSlow(&$highest_priority)
757         {
758                 $highest_priority = 0;
759
760                 $r = DBA::p(
761                         "SELECT `priority`
762                                 FROM `process`
763                                 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done`"
764                 );
765
766                 // No active processes at all? Fine
767                 if (!DBA::isResult($r)) {
768                         return false;
769                 }
770                 $priorities = [];
771                 while ($line = DBA::fetch($r)) {
772                         $priorities[] = $line["priority"];
773                 }
774                 DBA::close($r);
775
776                 // Should not happen
777                 if (count($priorities) == 0) {
778                         return false;
779                 }
780                 $highest_priority = min($priorities);
781
782                 // The highest process is already the slowest one?
783                 // Then we quit
784                 if ($highest_priority == PRIORITY_NEGLIGIBLE) {
785                         return false;
786                 }
787                 $high = 0;
788                 foreach ($priorities as $priority) {
789                         if ($priority == $highest_priority) {
790                                 ++$high;
791                         }
792                 }
793                 Logger::log("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, Logger::DEBUG);
794                 $passing_slow = (($high/count($priorities)) > (2/3));
795
796                 if ($passing_slow) {
797                         Logger::log("Passing slower processes than priority ".$highest_priority, Logger::DEBUG);
798                 }
799                 return $passing_slow;
800         }
801
802         /**
803          * @brief Find and claim the next worker process for us
804          *
805          * @param boolean $passing_slow Returns if we had passed low priority processes
806          * @return boolean Have we found something?
807          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
808          */
809         private static function findWorkerProcesses(&$passing_slow)
810         {
811                 $mypid = getmypid();
812
813                 // Check if we should pass some low priority process
814                 $highest_priority = 0;
815                 $found = false;
816                 $passing_slow = false;
817
818                 // The higher the number of parallel workers, the more we prefetch to prevent concurring access
819                 // We decrease the limit with the number of entries left in the queue
820                 $worker_queues = Config::get("system", "worker_queues", 4);
821                 $queue_length = Config::get('system', 'worker_fetch_limit', 1);
822                 $lower_job_limit = $worker_queues * $queue_length * 2;
823                 $jobs = self::totalEntries();
824                 $deferred = self::deferredEntries();
825
826                 // Now do some magic
827                 $exponent = 2;
828                 $slope = $queue_length / pow($lower_job_limit, $exponent);
829                 $limit = min($queue_length, ceil($slope * pow($jobs, $exponent)));
830
831                 Logger::log('Deferred: ' . $deferred . ' - Total: ' . $jobs . ' - Maximum: ' . $queue_length . ' - jobs per queue: ' . $limit, Logger::DEBUG);
832                 $ids = [];
833                 if (self::passingSlow($highest_priority)) {
834                         // Are there waiting processes with a higher priority than the currently highest?
835                         $result = DBA::select(
836                                 'workerqueue',
837                                 ['id'],
838                                 ["`executed` <= ? AND `priority` < ? AND NOT `done` AND `next_try` < ?",
839                                 DBA::NULL_DATETIME, $highest_priority, DateTimeFormat::utcNow()],
840                                 ['limit' => $limit, 'order' => ['priority', 'created']]
841                         );
842
843                         while ($id = DBA::fetch($result)) {
844                                 $ids[] = $id["id"];
845                         }
846                         DBA::close($result);
847
848                         $found = (count($ids) > 0);
849
850                         if (!$found) {
851                                 // Give slower processes some processing time
852                                 $result = DBA::select(
853                                         'workerqueue',
854                                         ['id'],
855                                         ["`executed` <= ? AND `priority` > ? AND NOT `done` AND `next_try` < ?",
856                                         DBA::NULL_DATETIME, $highest_priority, DateTimeFormat::utcNow()],
857                                         ['limit' => $limit, 'order' => ['priority', 'created']]
858                                 );
859
860                                 while ($id = DBA::fetch($result)) {
861                                         $ids[] = $id["id"];
862                                 }
863                                 DBA::close($result);
864
865                                 $found = (count($ids) > 0);
866                                 $passing_slow = $found;
867                         }
868                 }
869
870                 // If there is no result (or we shouldn't pass lower processes) we check without priority limit
871                 if (!$found) {
872                         $result = DBA::select(
873                                 'workerqueue',
874                                 ['id'],
875                                 ["`executed` <= ? AND NOT `done` AND `next_try` < ?",
876                                 DBA::NULL_DATETIME, DateTimeFormat::utcNow()],
877                                 ['limit' => $limit, 'order' => ['priority', 'created']]
878                         );
879
880                         while ($id = DBA::fetch($result)) {
881                                 $ids[] = $id["id"];
882                         }
883                         DBA::close($result);
884
885                         $found = (count($ids) > 0);
886                 }
887
888                 if ($found) {
889                         $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
890                         array_unshift($ids, $condition);
891                         DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $mypid], $ids);
892                 }
893
894                 return $found;
895         }
896
897         /**
898          * @brief Returns the next worker process
899          *
900          * @param boolean $passing_slow Returns if we had passed low priority processes
901          * @return string SQL statement
902          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
903          */
904         public static function workerProcess(&$passing_slow)
905         {
906                 $stamp = (float)microtime(true);
907
908                 // There can already be jobs for us in the queue.
909                 $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
910                 if (DBA::isResult($r)) {
911                         self::$db_duration += (microtime(true) - $stamp);
912                         return DBA::toArray($r);
913                 }
914                 DBA::close($r);
915
916                 $stamp = (float)microtime(true);
917                 if (!Lock::acquire('worker_process')) {
918                         return false;
919                 }
920                 self::$lock_duration = (microtime(true) - $stamp);
921
922                 $stamp = (float)microtime(true);
923                 $found = self::findWorkerProcesses($passing_slow);
924                 self::$db_duration += (microtime(true) - $stamp);
925
926                 Lock::release('worker_process');
927
928                 if ($found) {
929                         $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
930                         return DBA::toArray($r);
931                 }
932                 return false;
933         }
934
935         /**
936          * @brief Removes a workerqueue entry from the current process
937          * @return void
938          * @throws \Exception
939          */
940         public static function unclaimProcess()
941         {
942                 $mypid = getmypid();
943
944                 DBA::update('workerqueue', ['executed' => DBA::NULL_DATETIME, 'pid' => 0], ['pid' => $mypid, 'done' => false]);
945         }
946
947         /**
948          * @brief Call the front end worker
949          * @return void
950          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
951          */
952         public static function callWorker()
953         {
954                 if (!Config::get("system", "frontend_worker")) {
955                         return;
956                 }
957
958                 $url = System::baseUrl()."/worker";
959                 Network::fetchUrl($url, false, $redirects, 1);
960         }
961
962         /**
963          * @brief Call the front end worker if there aren't any active
964          * @return void
965          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
966          */
967         public static function executeIfIdle()
968         {
969                 if (!Config::get("system", "frontend_worker")) {
970                         return;
971                 }
972
973                 // Do we have "proc_open"? Then we can fork the worker
974                 if (function_exists("proc_open")) {
975                         // When was the last time that we called the worker?
976                         // Less than one minute? Then we quit
977                         if ((time() - Config::get("system", "worker_started")) < 60) {
978                                 return;
979                         }
980
981                         Config::set("system", "worker_started", time());
982
983                         // Do we have enough running workers? Then we quit here.
984                         if (self::tooMuchWorkers()) {
985                                 // Cleaning dead processes
986                                 self::killStaleWorkers();
987                                 Process::deleteInactive();
988
989                                 return;
990                         }
991
992                         self::runCron();
993
994                         Logger::log('Call worker', Logger::DEBUG);
995                         self::spawnWorker();
996                         return;
997                 }
998
999                 // We cannot execute background processes.
1000                 // We now run the processes from the frontend.
1001                 // This won't work with long running processes.
1002                 self::runCron();
1003
1004                 self::clearProcesses();
1005
1006                 $workers = self::activeWorkers();
1007
1008                 if ($workers == 0) {
1009                         self::callWorker();
1010                 }
1011         }
1012
1013         /**
1014          * @brief Removes long running worker processes
1015          * @return void
1016          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1017          */
1018         public static function clearProcesses()
1019         {
1020                 $timeout = Config::get("system", "frontend_worker_timeout", 10);
1021
1022                 /// @todo We should clean up the corresponding workerqueue entries as well
1023                 $condition = ["`created` < ? AND `command` = 'worker.php'",
1024                                 DateTimeFormat::utc("now - ".$timeout." minutes")];
1025                 DBA::delete('process', $condition);
1026         }
1027
1028         /**
1029          * @brief Runs the cron processes
1030          * @return void
1031          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1032          */
1033         private static function runCron()
1034         {
1035                 Logger::log('Add cron entries', Logger::DEBUG);
1036
1037                 // Check for spooled items
1038                 self::add(PRIORITY_HIGH, "SpoolPost");
1039
1040                 // Run the cron job that calls all other jobs
1041                 self::add(PRIORITY_MEDIUM, "Cron");
1042
1043                 // Cleaning dead processes
1044                 self::killStaleWorkers();
1045         }
1046
1047         /**
1048          * @brief Spawns a new worker
1049          * @param bool $do_cron
1050          * @return void
1051          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1052          */
1053         public static function spawnWorker($do_cron = false)
1054         {
1055                 $command = 'bin/worker.php';
1056
1057                 $args = ['no_cron' => !$do_cron];
1058
1059                 get_app()->proc_run($command, $args);
1060
1061                 // after spawning we have to remove the flag.
1062                 if (Config::get('system', 'worker_daemon_mode', false)) {
1063                         self::IPCSetJobState(false);
1064                 }
1065         }
1066
1067         /**
1068          * @brief Adds tasks to the worker queue
1069          *
1070          * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
1071          *
1072          * next args are passed as $cmd command line
1073          * or: Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
1074          * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
1075          *
1076          * @return boolean "false" if proc_run couldn't be executed
1077          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1078          * @note $cmd and string args are surrounded with ""
1079          *
1080          * @hooks 'proc_run'
1081          *    array $arr
1082          *
1083          */
1084         public static function add($cmd)
1085         {
1086                 $args = func_get_args();
1087
1088                 if (!count($args)) {
1089                         return false;
1090                 }
1091
1092                 $arr = ['args' => $args, 'run_cmd' => true];
1093
1094                 Hook::callAll("proc_run", $arr);
1095                 if (!$arr['run_cmd'] || !count($args)) {
1096                         return true;
1097                 }
1098
1099                 $priority = PRIORITY_MEDIUM;
1100                 $dont_fork = Config::get("system", "worker_dont_fork", false);
1101                 $created = DateTimeFormat::utcNow();
1102
1103                 $run_parameter = array_shift($args);
1104
1105                 if (is_int($run_parameter)) {
1106                         $priority = $run_parameter;
1107                 } elseif (is_array($run_parameter)) {
1108                         if (isset($run_parameter['priority'])) {
1109                                 $priority = $run_parameter['priority'];
1110                         }
1111                         if (isset($run_parameter['created'])) {
1112                                 $created = $run_parameter['created'];
1113                         }
1114                         if (isset($run_parameter['dont_fork'])) {
1115                                 $dont_fork = $run_parameter['dont_fork'];
1116                         }
1117                 }
1118
1119                 $parameters = json_encode($args);
1120                 $found = DBA::exists('workerqueue', ['parameter' => $parameters, 'done' => false]);
1121
1122                 // Quit if there was a database error - a precaution for the update process to 3.5.3
1123                 if (DBA::errorNo() != 0) {
1124                         return false;
1125                 }
1126
1127                 if (!$found) {
1128                         DBA::insert('workerqueue', ['parameter' => $parameters, 'created' => $created, 'priority' => $priority]);
1129                 }
1130
1131                 // Should we quit and wait for the worker to be called as a cronjob?
1132                 if ($dont_fork) {
1133                         return true;
1134                 }
1135
1136                 // If there is a lock then we don't have to check for too much worker
1137                 if (!Lock::acquire('worker', 0)) {
1138                         return true;
1139                 }
1140
1141                 // If there are already enough workers running, don't fork another one
1142                 $quit = self::tooMuchWorkers();
1143                 Lock::release('worker');
1144
1145                 if ($quit) {
1146                         return true;
1147                 }
1148
1149                 // We tell the daemon that a new job entry exists
1150                 if (Config::get('system', 'worker_daemon_mode', false)) {
1151                         // We don't have to set the IPC flag - this is done in "tooMuchWorkers"
1152                         return true;
1153                 }
1154
1155                 // Now call the worker to execute the jobs that we just added to the queue
1156                 self::spawnWorker();
1157
1158                 return true;
1159         }
1160
1161         /**
1162          * Defers the current worker entry
1163          */
1164         public static function defer()
1165         {
1166                 if (empty(BaseObject::getApp()->queue)) {
1167                         return;
1168                 }
1169
1170                 $queue = BaseObject::getApp()->queue;
1171
1172                 $retrial = $queue['retrial'];
1173                 $id = $queue['id'];
1174
1175                 if ($retrial > 14) {
1176                         Logger::log('Id ' . $id . ' had been tried 14 times. We stop now.', Logger::DEBUG);
1177                         return;
1178                 }
1179
1180                 // Calculate the delay until the next trial
1181                 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
1182                 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
1183
1184                 Logger::log('Defer execution ' . $retrial . ' of id ' . $id . ' to ' . $next, Logger::DEBUG);
1185
1186                 $fields = ['retrial' => $retrial + 1, 'next_try' => $next, 'executed' => DBA::NULL_DATETIME, 'pid' => 0];
1187                 DBA::update('workerqueue', $fields, ['id' => $id]);
1188         }
1189
1190         /**
1191          * Log active processes into the "process" table
1192          *
1193          * @brief Log active processes into the "process" table
1194          */
1195         public static function startProcess()
1196         {
1197                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
1198
1199                 $command = basename($trace[0]['file']);
1200
1201                 Process::deleteInactive();
1202
1203                 Process::insert($command);
1204         }
1205
1206         /**
1207          * Remove the active process from the "process" table
1208          *
1209          * @brief Remove the active process from the "process" table
1210          * @return bool
1211          * @throws \Exception
1212          */
1213         public static function endProcess()
1214         {
1215                 return Process::deleteByPid();
1216         }
1217
1218         /**
1219          * Set the flag if some job is waiting
1220          *
1221          * @brief Set the flag if some job is waiting
1222          * @param boolean $jobs Is there a waiting job?
1223          * @throws \Exception
1224          */
1225         public static function IPCSetJobState($jobs)
1226         {
1227                 DBA::update('worker-ipc', ['jobs' => $jobs], ['key' => 1], true);
1228         }
1229
1230         /**
1231          * Checks if some worker job waits to be executed
1232          *
1233          * @brief Checks if some worker job waits to be executed
1234          * @return bool
1235          * @throws \Exception
1236          */
1237         public static function IPCJobsExists()
1238         {
1239                 $row = DBA::selectFirst('worker-ipc', ['jobs'], ['key' => 1]);
1240
1241                 // When we don't have a row, no job is running
1242                 if (!DBA::isResult($row)) {
1243                         return false;
1244                 }
1245
1246                 return (bool)$row['jobs'];
1247         }
1248 }