3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Core\System;
7 use Friendica\Core\Worker as WorkerCore;
8 use Friendica\Database\DBA;
10 use Friendica\Util\DateTimeFormat;
13 * Module for starting the backend worker through a frontend call
15 class Worker extends BaseModule
17 public static function rawContent(array $parameters = [])
19 if (!DI::config()->get("system", "frontend_worker")) {
23 // Ensure that all "strtotime" operations do run timezone independent
24 date_default_timezone_set('UTC');
26 // We don't need the following lines if we can execute background jobs.
27 // So we just wake up the worker if it sleeps.
28 if (function_exists("proc_open")) {
29 WorkerCore::executeIfIdle();
33 WorkerCore::clearProcesses();
35 $workers = DBA::count('process', ['command' => 'worker.php']);
37 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
41 WorkerCore::startProcess();
43 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
45 WorkerCore::callWorker();
47 if ($r = WorkerCore::workerProcess()) {
48 // On most configurations this parameter wouldn't have any effect.
49 // But since it doesn't destroy anything, we just try to get more execution time in any way.
52 $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
53 $condition = ['id' => $r[0]["id"], 'pid' => 0];
54 if (DBA::update('workerqueue', $fields, $condition)) {
55 WorkerCore::execute($r[0]);
59 WorkerCore::callWorker();
61 WorkerCore::unclaimProcess();
63 WorkerCore::endProcess();
65 System::httpExit(200, 'Frontend worker stopped.');