3 * @file src/Core/Worker.php
5 namespace Friendica\Core;
7 use Friendica\Database\DBA;
8 use Friendica\Model\Process;
9 use Friendica\Util\DateTimeFormat;
10 use Friendica\Util\Network;
12 require_once 'include/dba.php';
15 * @file src/Core/Worker.php
17 * @brief Contains the class for the worker background job processing
21 * @brief Worker methods
25 private static $up_start;
26 private static $db_duration;
27 private static $last_update;
28 private static $lock_duration;
31 * @brief Processes the tasks that are in the workerqueue table
33 * @param boolean $run_cron Should the cron processes be executed?
36 public static function processQueue($run_cron = true)
40 self::$up_start = microtime(true);
42 // At first check the maximum load. We shouldn't continue with a high load
43 if ($a->isMaxLoadReached()) {
44 logger('Pre check: maximum load reached, quitting.', LOGGER_DEBUG);
48 // We now start the process. This is done after the load check since this could increase the load.
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();
58 // Count active workers and compare them with a maximum value that depends on the load
59 if (self::tooMuchWorkers()) {
60 logger('Pre check: Active worker limit reached, quitting.', LOGGER_DEBUG);
64 // Do we have too few memory?
65 if ($a->min_memory_reached()) {
66 logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
70 // Possibly there are too much database connections
71 if (self::maxConnectionsReached()) {
72 logger('Pre check: maximum connections reached, quitting.', LOGGER_DEBUG);
76 // Possibly there are too much database processes that block the system
77 if ($a->isMaxProcessesReached()) {
78 logger('Pre check: maximum processes reached, quitting.', LOGGER_DEBUG);
82 // Now we start additional cron processes if we should do so
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;
95 foreach ($r as $entry) {
96 // Assure that the priority is an integer value
97 $entry['priority'] = (int)$entry['priority'];
99 // The work will be done
100 if (!self::execute($entry)) {
101 logger('Process execution failed, quitting.', LOGGER_DEBUG);
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');
114 // To avoid the quitting of multiple workers only one worker at a time will execute the check
115 if (Lock::acquire('worker', 0)) {
116 $stamp = (float)microtime(true);
117 // Count active workers and compare them with a maximum value that depends on the load
118 if (self::tooMuchWorkers()) {
119 logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
120 Lock::release('worker');
125 if ($a->min_memory_reached()) {
126 logger('Memory limit reached, quitting.', LOGGER_DEBUG);
127 Lock::release('worker');
130 Lock::release('worker');
131 self::$db_duration += (microtime(true) - $stamp);
134 // Quit the worker once every 5 minutes
135 if (time() > ($starttime + 300)) {
136 logger('Process lifetime reached, quitting.', LOGGER_DEBUG);
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);
145 logger("Couldn't select a workerqueue entry, quitting process " . getmypid() . ".", LOGGER_DEBUG);
149 * @brief Returns the number of non executed entries in the worker queue
151 * @return integer Number of non executed entries in the worker queue
153 private static function totalEntries()
155 return DBA::count('workerqueue', ["`executed` <= ? AND NOT `done`", NULL_DATE]);
159 * @brief Returns the highest priority in the worker queue that isn't executed
161 * @return integer Number of active worker processes
163 private static function highestPriority()
165 $condition = ["`executed` <= ? AND NOT `done`", NULL_DATE];
166 $workerqueue = DBA::selectFirst('workerqueue', ['priority'], $condition, ['order' => ['priority']]);
167 if (DBA::isResult($workerqueue)) {
168 return $workerqueue["priority"];
175 * @brief Returns if a process with the given priority is running
177 * @param integer $priority The priority that should be checked
179 * @return integer Is there a process running with that priority?
181 private static function processWithPriorityActive($priority)
183 $condition = ["`priority` <= ? AND `executed` > ? AND NOT `done`", $priority, NULL_DATE];
184 return DBA::exists('workerqueue', $condition);
188 * @brief Execute a worker entry
190 * @param array $queue Workerqueue entry
192 * @return boolean "true" if further processing should be stopped
194 public static function execute($queue)
200 // Quit when in maintenance
201 if (Config::get('system', 'maintenance', false, true)) {
202 logger("Maintenance mode - quit process ".$mypid, LOGGER_DEBUG);
206 // Constantly check the number of parallel database processes
207 if ($a->isMaxProcessesReached()) {
208 logger("Max processes reached for process ".$mypid, LOGGER_DEBUG);
212 // Constantly check the number of available database connections to let the frontend be accessible at any time
213 if (self::maxConnectionsReached()) {
214 logger("Max connection reached for process ".$mypid, LOGGER_DEBUG);
218 $argv = json_decode($queue["parameter"], true);
220 // Check for existance and validity of the include file
223 if (method_exists(sprintf('Friendica\Worker\%s', $include), 'execute')) {
224 // We constantly update the "executed" date every minute to avoid being killed too soon
225 if (!isset(self::$last_update)) {
226 self::$last_update = strtotime($queue["executed"]);
229 $age = (time() - self::$last_update) / 60;
230 self::$last_update = time();
233 $stamp = (float)microtime(true);
234 DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
235 self::$db_duration += (microtime(true) - $stamp);
240 self::execFunction($queue, $include, $argv, true);
242 $stamp = (float)microtime(true);
243 if (DBA::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
244 Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
246 self::$db_duration = (microtime(true) - $stamp);
251 // The script could be provided as full path or only with the function name
252 if ($include == basename($include)) {
253 $include = "include/".$include.".php";
256 if (!validate_include($include)) {
257 logger("Include file ".$argv[0]." is not valid!");
258 DBA::delete('workerqueue', ['id' => $queue["id"]]);
262 require_once $include;
264 $funcname = str_replace(".php", "", basename($argv[0]))."_run";
266 if (function_exists($funcname)) {
267 // We constantly update the "executed" date every minute to avoid being killed too soon
268 if (!isset(self::$last_update)) {
269 self::$last_update = strtotime($queue["executed"]);
272 $age = (time() - self::$last_update) / 60;
273 self::$last_update = time();
276 $stamp = (float)microtime(true);
277 DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow()], ['pid' => $mypid, 'done' => false]);
278 self::$db_duration += (microtime(true) - $stamp);
281 self::execFunction($queue, $funcname, $argv, false);
283 $stamp = (float)microtime(true);
284 if (DBA::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
285 Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
287 self::$db_duration = (microtime(true) - $stamp);
289 logger("Function ".$funcname." does not exist");
290 DBA::delete('workerqueue', ['id' => $queue["id"]]);
297 * @brief Execute a function from the queue
299 * @param array $queue Workerqueue entry
300 * @param string $funcname name of the function
301 * @param array $argv Array of values to be passed to the function
302 * @param boolean $method_call boolean
305 private static function execFunction($queue, $funcname, $argv, $method_call)
311 $argc = count($argv);
313 $new_process_id = System::processID("wrk");
315 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
317 $stamp = (float)microtime(true);
319 // We use the callstack here to analyze the performance of executed worker entries.
320 // For this reason the variables have to be initialized.
321 if (Config::get("system", "profiler")) {
322 $a->performance["start"] = microtime(true);
323 $a->performance["database"] = 0;
324 $a->performance["database_write"] = 0;
325 $a->performance["cache"] = 0;
326 $a->performance["cache_write"] = 0;
327 $a->performance["network"] = 0;
328 $a->performance["file"] = 0;
329 $a->performance["rendering"] = 0;
330 $a->performance["parser"] = 0;
331 $a->performance["marktime"] = 0;
332 $a->performance["markstart"] = microtime(true);
336 // For better logging create a new process id for every worker call
337 // But preserve the old one for the worker
338 $old_process_id = $a->process_id;
339 $a->process_id = $new_process_id;
342 $up_duration = number_format(microtime(true) - self::$up_start, 3);
344 // Reset global data to avoid interferences
348 call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv);
350 $funcname($argv, $argc);
353 $a->process_id = $old_process_id;
356 $duration = (microtime(true) - $stamp);
358 self::$up_start = microtime(true);
360 /* With these values we can analyze how effective the worker is.
361 * The database and rest time should be low since this is the unproductive time.
362 * The execution time is the productive time.
363 * By changing parameters like the maximum number of workers we can check the effectivness.
366 'DB: '.number_format(self::$db_duration, 2).
367 ' - Lock: '.number_format(self::$lock_duration, 2).
368 ' - Rest: '.number_format($up_duration - self::$db_duration - self::$lock_duration, 2).
369 ' - Execution: '.number_format($duration, 2),
373 self::$lock_duration = 0;
375 if ($duration > 3600) {
376 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 1 hour (".round($duration/60, 3).")", LOGGER_DEBUG);
377 } elseif ($duration > 600) {
378 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 10 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
379 } elseif ($duration > 300) {
380 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 5 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
381 } elseif ($duration > 120) {
382 logger("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", LOGGER_DEBUG);
385 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds. Process PID: ".$new_process_id);
387 // Write down the performance values into the log
388 if (Config::get("system", "profiler")) {
389 $duration = microtime(true)-$a->performance["start"];
392 if (Config::get("rendertime", "callstack")) {
393 if (isset($a->callstack["database"])) {
394 $o .= "\nDatabase Read:\n";
395 foreach ($a->callstack["database"] as $func => $time) {
396 $time = round($time, 3);
398 $o .= $func.": ".$time."\n";
402 if (isset($a->callstack["database_write"])) {
403 $o .= "\nDatabase Write:\n";
404 foreach ($a->callstack["database_write"] as $func => $time) {
405 $time = round($time, 3);
407 $o .= $func.": ".$time."\n";
411 if (isset($a->callstack["dache"])) {
412 $o .= "\nCache Read:\n";
413 foreach ($a->callstack["dache"] as $func => $time) {
414 $time = round($time, 3);
416 $o .= $func.": ".$time."\n";
420 if (isset($a->callstack["dache_write"])) {
421 $o .= "\nCache Write:\n";
422 foreach ($a->callstack["dache_write"] as $func => $time) {
423 $time = round($time, 3);
425 $o .= $func.": ".$time."\n";
429 if (isset($a->callstack["network"])) {
430 $o .= "\nNetwork:\n";
431 foreach ($a->callstack["network"] as $func => $time) {
432 $time = round($time, 3);
434 $o .= $func.": ".$time."\n";
441 "ID ".$queue["id"].": ".$funcname.": ".sprintf(
442 "DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
443 number_format($a->performance["database"] - $a->performance["database_write"], 2),
444 number_format($a->performance["database_write"], 2),
445 number_format($a->performance["cache"], 2),
446 number_format($a->performance["cache_write"], 2),
447 number_format($a->performance["network"], 2),
448 number_format($a->performance["file"], 2),
449 number_format($duration - ($a->performance["database"]
450 + $a->performance["cache"] + $a->performance["cache_write"]
451 + $a->performance["network"] + $a->performance["file"]), 2),
452 number_format($duration, 2)
458 $cooldown = Config::get("system", "worker_cooldown", 0);
461 logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
467 * @brief Checks if the number of database connections has reached a critical limit.
469 * @return bool Are more than 3/4 of the maximum connections used?
471 private static function maxConnectionsReached()
473 // Fetch the max value from the config. This is needed when the system cannot detect the correct value by itself.
474 $max = Config::get("system", "max_connections");
476 // Fetch the percentage level where the worker will get active
477 $maxlevel = Config::get("system", "max_connections_level", 75);
480 // the maximum number of possible user connections can be a system variable
481 $r = DBA::fetchFirst("SHOW VARIABLES WHERE `variable_name` = 'max_user_connections'");
482 if (DBA::isResult($r)) {
485 // Or it can be granted. This overrides the system variable
486 $r = DBA::p('SHOW GRANTS');
487 while ($grants = DBA::fetch($r)) {
488 $grant = array_pop($grants);
489 if (stristr($grant, "GRANT USAGE ON")) {
490 if (preg_match("/WITH MAX_USER_CONNECTIONS (\d*)/", $grant, $match)) {
498 // If $max is set we will use the processlist to determine the current number of connections
499 // The processlist only shows entries of the current user
501 $r = DBA::p('SHOW PROCESSLIST');
502 $used = DBA::numRows($r);
505 logger("Connection usage (user values): ".$used."/".$max, LOGGER_DEBUG);
507 $level = ($used / $max) * 100;
509 if ($level >= $maxlevel) {
510 logger("Maximum level (".$maxlevel."%) of user connections reached: ".$used."/".$max);
515 // We will now check for the system values.
516 // This limit could be reached although the user limits are fine.
517 $r = DBA::fetchFirst("SHOW VARIABLES WHERE `variable_name` = 'max_connections'");
518 if (!DBA::isResult($r)) {
521 $max = intval($r["Value"]);
525 $r = DBA::fetchFirst("SHOW STATUS WHERE `variable_name` = 'Threads_connected'");
526 if (!DBA::isResult($r)) {
529 $used = intval($r["Value"]);
533 logger("Connection usage (system values): ".$used."/".$max, LOGGER_DEBUG);
535 $level = $used / $max * 100;
537 if ($level < $maxlevel) {
540 logger("Maximum level (".$level."%) of system connections reached: ".$used."/".$max);
545 * @brief fix the queue entry if the worker process died
548 private static function killStaleWorkers()
550 $entries = DBA::select(
552 ['id', 'pid', 'executed', 'priority', 'parameter'],
553 ['`executed` > ? AND NOT `done` AND `pid` != 0', NULL_DATE],
554 ['order' => ['priority', 'created']]
557 while ($entry = DBA::fetch($entries)) {
558 if (!posix_kill($entry["pid"], 0)) {
561 ['executed' => NULL_DATE, 'pid' => 0],
562 ['id' => $entry["id"]]
565 // Kill long running processes
566 // Check if the priority is in a valid range
567 if (!in_array($entry["priority"], [PRIORITY_CRITICAL, PRIORITY_HIGH, PRIORITY_MEDIUM, PRIORITY_LOW, PRIORITY_NEGLIGIBLE])) {
568 $entry["priority"] = PRIORITY_MEDIUM;
571 // Define the maximum durations
572 $max_duration_defaults = [PRIORITY_CRITICAL => 720, PRIORITY_HIGH => 10, PRIORITY_MEDIUM => 60, PRIORITY_LOW => 180, PRIORITY_NEGLIGIBLE => 720];
573 $max_duration = $max_duration_defaults[$entry["priority"]];
575 $argv = json_decode($entry["parameter"], true);
576 $argv[0] = basename($argv[0]);
578 // How long is the process already running?
579 $duration = (time() - strtotime($entry["executed"])) / 60;
580 if ($duration > $max_duration) {
581 logger("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") took more than ".$max_duration." minutes. It will be killed now.");
582 posix_kill($entry["pid"], SIGTERM);
584 // We killed the stale process.
585 // To avoid a blocking situation we reschedule the process at the beginning of the queue.
586 // Additionally we are lowering the priority. (But not PRIORITY_CRITICAL)
587 $new_priority = $entry["priority"];
588 if ($entry["priority"] == PRIORITY_HIGH) {
589 $new_priority = PRIORITY_MEDIUM;
590 } elseif ($entry["priority"] == PRIORITY_MEDIUM) {
591 $new_priority = PRIORITY_LOW;
592 } elseif ($entry["priority"] != PRIORITY_CRITICAL) {
593 $new_priority = PRIORITY_NEGLIGIBLE;
597 ['executed' => NULL_DATE, 'created' => DateTimeFormat::utcNow(), 'priority' => $new_priority, 'pid' => 0],
598 ['id' => $entry["id"]]
601 logger("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") now runs for ".round($duration)." of ".$max_duration." allowed minutes. That's okay.", LOGGER_DEBUG);
608 * @brief Checks if the number of active workers exceeds the given limits
610 * @return bool Are there too much workers running?
612 public static function tooMuchWorkers()
614 $queues = Config::get("system", "worker_queues", 4);
616 $maxqueues = $queues;
618 $active = self::activeWorkers();
620 // Decrease the number of workers at higher load
621 $load = current_load();
623 $maxsysload = intval(Config::get("system", "maxloadavg", 50));
625 /* Default exponent 3 causes queues to rapidly decrease as load increases.
626 * If you have 20 max queues at idle, then you get only 5 queues at 37.1% of $maxsysload.
627 * For some environments, this rapid decrease is not needed.
628 * With exponent 1, you could have 20 max queues at idle and 13 at 37% of $maxsysload.
630 $exponent = intval(Config::get('system', 'worker_load_exponent', 3));
631 $slope = pow(max(0, $maxsysload - $load) / $maxsysload, $exponent);
632 $queues = intval(ceil($slope * $maxqueues));
636 if (Config::get('system', 'worker_debug')) {
637 // Create a list of queue entries grouped by their priority
640 // Adding all processes with no workerqueue entry
642 "SELECT COUNT(*) AS `running` FROM `process` WHERE NOT EXISTS
643 (SELECT id FROM `workerqueue`
644 WHERE `workerqueue`.`pid` = `process`.`pid` AND NOT `done` AND `pid` != ?)",
648 if ($process = DBA::fetch($processes)) {
649 $listitem[0] = "0:".$process["running"];
651 DBA::close($processes);
653 // Now adding all processes with workerqueue entries
654 $entries = DBA::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` GROUP BY `priority`");
655 while ($entry = DBA::fetch($entries)) {
656 $processes = DBA::p("SELECT COUNT(*) AS `running` FROM `process` INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done` WHERE `priority` = ?", $entry["priority"]);
657 if ($process = DBA::fetch($processes)) {
658 $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
660 DBA::close($processes);
662 DBA::close($entries);
664 $intervals = [1, 10, 60];
665 $jobs_per_minute = [];
666 foreach ($intervals as $interval) {
667 $jobs = DBA::p("SELECT COUNT(*) AS `jobs` FROM `workerqueue` WHERE `done` AND `executed` > UTC_TIMESTAMP() - INTERVAL ".intval($interval)." MINUTE");
668 if ($job = DBA::fetch($jobs)) {
669 $jobs_per_minute[$interval] = number_format($job['jobs'] / $interval, 0);
673 $processlist = ' - jpm: '.implode('/', $jobs_per_minute).' ('.implode(', ', $listitem).')';
676 $entries = self::totalEntries();
678 if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && ($entries > 0) && ($active >= $queues)) {
679 $top_priority = self::highestPriority();
680 $high_running = self::processWithPriorityActive($top_priority);
682 if (!$high_running && ($top_priority > PRIORITY_UNDEFINED) && ($top_priority < PRIORITY_NEGLIGIBLE)) {
683 logger("There are jobs with priority ".$top_priority." waiting but none is executed. Open a fastlane.", LOGGER_DEBUG);
684 $queues = $active + 1;
688 logger("Load: ".$load."/".$maxsysload." - processes: ".$active."/".$entries.$processlist." - maximum: ".$queues."/".$maxqueues, LOGGER_DEBUG);
690 // Are there fewer workers running as possible? Then fork a new one.
691 if (!Config::get("system", "worker_dont_fork", false) && ($queues > ($active + 1)) && ($entries > 1)) {
692 logger("Active workers: ".$active."/".$queues." Fork a new worker.", LOGGER_DEBUG);
693 if (Config::get('system', 'worker_daemon_mode', false)) {
694 self::IPCSetJobState(true);
701 // if there are too much worker, we don't spawn a new one.
702 if (Config::get('system', 'worker_daemon_mode', false) && ($active > $queues)) {
703 self::IPCSetJobState(false);
706 return $active > $queues;
710 * @brief Returns the number of active worker processes
712 * @return integer Number of active worker processes
714 private static function activeWorkers()
716 return DBA::count('process', ['command' => 'Worker.php']);
720 * @brief Check if we should pass some slow processes
722 * When the active processes of the highest priority are using more than 2/3
723 * of all processes, we let pass slower processes.
725 * @param string $highest_priority Returns the currently highest priority
726 * @return bool We let pass a slower process than $highest_priority
728 private static function passingSlow(&$highest_priority)
730 $highest_priority = 0;
735 INNER JOIN `workerqueue` ON `workerqueue`.`pid` = `process`.`pid` AND NOT `done`"
738 // No active processes at all? Fine
739 if (!DBA::isResult($r)) {
743 while ($line = DBA::fetch($r)) {
744 $priorities[] = $line["priority"];
749 if (count($priorities) == 0) {
752 $highest_priority = min($priorities);
754 // The highest process is already the slowest one?
756 if ($highest_priority == PRIORITY_NEGLIGIBLE) {
760 foreach ($priorities as $priority) {
761 if ($priority == $highest_priority) {
765 logger("Highest priority: ".$highest_priority." Total processes: ".count($priorities)." Count high priority processes: ".$high, LOGGER_DEBUG);
766 $passing_slow = (($high/count($priorities)) > (2/3));
769 logger("Passing slower processes than priority ".$highest_priority, LOGGER_DEBUG);
771 return $passing_slow;
775 * @brief Find and claim the next worker process for us
777 * @param boolean $passing_slow Returns if we had passed low priority processes
778 * @return boolean Have we found something?
780 private static function findWorkerProcesses(&$passing_slow)
784 // Check if we should pass some low priority process
785 $highest_priority = 0;
787 $passing_slow = false;
789 // The higher the number of parallel workers, the more we prefetch to prevent concurring access
790 // We decrease the limit with the number of entries left in the queue
791 $worker_queues = Config::get("system", "worker_queues", 4);
792 $queue_length = Config::get('system', 'worker_fetch_limit', 1);
793 $lower_job_limit = $worker_queues * $queue_length * 2;
794 $jobs = self::totalEntries();
798 $slope = $queue_length / pow($lower_job_limit, $exponent);
799 $limit = min($queue_length, ceil($slope * pow($jobs, $exponent)));
801 logger('Total: '.$jobs.' - Maximum: '.$queue_length.' - jobs per queue: '.$limit, LOGGER_DEBUG);
803 if (self::passingSlow($highest_priority)) {
804 // Are there waiting processes with a higher priority than the currently highest?
805 $result = DBA::select(
808 ["`executed` <= ? AND `priority` < ? AND NOT `done`", NULL_DATE, $highest_priority],
809 ['limit' => $limit, 'order' => ['priority', 'created']]
812 while ($id = DBA::fetch($result)) {
817 $found = (count($ids) > 0);
820 // Give slower processes some processing time
821 $result = DBA::select(
824 ["`executed` <= ? AND `priority` > ? AND NOT `done`", NULL_DATE, $highest_priority],
825 ['limit' => $limit, 'order' => ['priority', 'created']]
828 while ($id = DBA::fetch($result)) {
833 $found = (count($ids) > 0);
834 $passing_slow = $found;
838 // If there is no result (or we shouldn't pass lower processes) we check without priority limit
840 $result = DBA::select(
843 ["`executed` <= ? AND NOT `done`", NULL_DATE],
844 ['limit' => $limit, 'order' => ['priority', 'created']]
847 while ($id = DBA::fetch($result)) {
852 $found = (count($ids) > 0);
856 $condition = "`id` IN (".substr(str_repeat("?, ", count($ids)), 0, -2).") AND `pid` = 0 AND NOT `done`";
857 array_unshift($ids, $condition);
858 DBA::update('workerqueue', ['executed' => DateTimeFormat::utcNow(), 'pid' => $mypid], $ids);
865 * @brief Returns the next worker process
867 * @param boolean $passing_slow Returns if we had passed low priority processes
868 * @return string SQL statement
870 public static function workerProcess(&$passing_slow)
872 $stamp = (float)microtime(true);
874 // There can already be jobs for us in the queue.
875 $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
876 if (DBA::isResult($r)) {
877 self::$db_duration += (microtime(true) - $stamp);
878 return DBA::toArray($r);
882 $stamp = (float)microtime(true);
883 if (!Lock::acquire('worker_process')) {
886 self::$lock_duration = (microtime(true) - $stamp);
888 $stamp = (float)microtime(true);
889 $found = self::findWorkerProcesses($passing_slow);
890 self::$db_duration += (microtime(true) - $stamp);
892 Lock::release('worker_process');
895 $r = DBA::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
896 return DBA::toArray($r);
902 * @brief Removes a workerqueue entry from the current process
905 public static function unclaimProcess()
909 DBA::update('workerqueue', ['executed' => NULL_DATE, 'pid' => 0], ['pid' => $mypid, 'done' => false]);
913 * @brief Call the front end worker
916 public static function callWorker()
918 if (!Config::get("system", "frontend_worker")) {
922 $url = System::baseUrl()."/worker";
923 Network::fetchUrl($url, false, $redirects, 1);
927 * @brief Call the front end worker if there aren't any active
930 public static function executeIfIdle()
932 if (!Config::get("system", "frontend_worker")) {
936 // Do we have "proc_open"? Then we can fork the worker
937 if (function_exists("proc_open")) {
938 // When was the last time that we called the worker?
939 // Less than one minute? Then we quit
940 if ((time() - Config::get("system", "worker_started")) < 60) {
944 Config::set("system", "worker_started", time());
946 // Do we have enough running workers? Then we quit here.
947 if (self::tooMuchWorkers()) {
948 // Cleaning dead processes
949 self::killStaleWorkers();
950 Process::deleteInactive();
957 logger('Call worker', LOGGER_DEBUG);
962 // We cannot execute background processes.
963 // We now run the processes from the frontend.
964 // This won't work with long running processes.
967 self::clearProcesses();
969 $workers = self::activeWorkers();
977 * @brief Removes long running worker processes
980 public static function clearProcesses()
982 $timeout = Config::get("system", "frontend_worker_timeout", 10);
984 /// @todo We should clean up the corresponding workerqueue entries as well
985 $condition = ["`created` < ? AND `command` = 'worker.php'",
986 DateTimeFormat::utc("now - ".$timeout." minutes")];
987 DBA::delete('process', $condition);
991 * @brief Runs the cron processes
994 private static function runCron()
996 logger('Add cron entries', LOGGER_DEBUG);
998 // Check for spooled items
999 self::add(PRIORITY_HIGH, "SpoolPost");
1001 // Run the cron job that calls all other jobs
1002 self::add(PRIORITY_MEDIUM, "Cron");
1004 // Cleaning dead processes
1005 self::killStaleWorkers();
1009 * @brief Spawns a new worker
1012 public static function spawnWorker($do_cron = false)
1014 $command = 'bin/worker.php';
1016 $args = ['no_cron' => !$do_cron];
1018 get_app()->proc_run($command, $args);
1020 // after spawning we have to remove the flag.
1021 if (Config::get('system', 'worker_daemon_mode', false)) {
1022 self::IPCSetJobState(false);
1027 * @brief Adds tasks to the worker queue
1029 * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
1031 * next args are passed as $cmd command line
1032 * or: Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
1033 * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
1035 * @note $cmd and string args are surrounded with ""
1040 * @return boolean "false" if proc_run couldn't be executed
1042 public static function add($cmd)
1044 $args = func_get_args();
1046 if (!count($args)) {
1050 $arr = ['args' => $args, 'run_cmd' => true];
1052 Addon::callHooks("proc_run", $arr);
1053 if (!$arr['run_cmd'] || !count($args)) {
1057 $priority = PRIORITY_MEDIUM;
1058 $dont_fork = Config::get("system", "worker_dont_fork", false);
1059 $created = DateTimeFormat::utcNow();
1061 $run_parameter = array_shift($args);
1063 if (is_int($run_parameter)) {
1064 $priority = $run_parameter;
1065 } elseif (is_array($run_parameter)) {
1066 if (isset($run_parameter['priority'])) {
1067 $priority = $run_parameter['priority'];
1069 if (isset($run_parameter['created'])) {
1070 $created = $run_parameter['created'];
1072 if (isset($run_parameter['dont_fork'])) {
1073 $dont_fork = $run_parameter['dont_fork'];
1077 $parameters = json_encode($args);
1078 $found = DBA::exists('workerqueue', ['parameter' => $parameters, 'done' => false]);
1080 // Quit if there was a database error - a precaution for the update process to 3.5.3
1081 if (DBA::errorNo() != 0) {
1086 DBA::insert('workerqueue', ['parameter' => $parameters, 'created' => $created, 'priority' => $priority]);
1089 // Should we quit and wait for the worker to be called as a cronjob?
1094 // If there is a lock then we don't have to check for too much worker
1095 if (!Lock::acquire('worker', 0)) {
1099 // If there are already enough workers running, don't fork another one
1100 $quit = self::tooMuchWorkers();
1101 Lock::release('worker');
1107 // We tell the daemon that a new job entry exists
1108 if (Config::get('system', 'worker_daemon_mode', false)) {
1109 // We don't have to set the IPC flag - this is done in "tooMuchWorkers"
1113 // Now call the worker to execute the jobs that we just added to the queue
1114 self::spawnWorker();
1120 * Log active processes into the "process" table
1122 * @brief Log active processes into the "process" table
1124 public static function startProcess()
1126 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
1128 $command = basename($trace[0]['file']);
1130 Process::deleteInactive();
1132 Process::insert($command);
1136 * Remove the active process from the "process" table
1138 * @brief Remove the active process from the "process" table
1141 public static function endProcess()
1143 return Process::deleteByPid();
1147 * Set the flag if some job is waiting
1149 * @brief Set the flag if some job is waiting
1150 * @param boolean $jobs Is there a waiting job?
1152 public static function IPCSetJobState($jobs)
1154 DBA::update('worker-ipc', ['jobs' => $jobs], ['key' => 1], true);
1158 * Checks if some worker job waits to be executed
1160 * @brief Checks if some worker job waits to be executed
1163 public static function IPCJobsExists()
1165 $row = DBA::selectFirst('worker-ipc', ['jobs'], ['key' => 1]);
1167 // When we don't have a row, no job is running
1168 if (!DBA::isResult($row)) {
1172 return (bool)$row['jobs'];