4 * @brief Module for running the worker as frontend process
7 use Friendica\Core\Config;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBA;
10 use Friendica\Util\DateTimeFormat;
12 function worker_init()
15 if (!Config::get("system", "frontend_worker")) {
19 // We don't need the following lines if we can execute background jobs.
20 // So we just wake up the worker if it sleeps.
21 if (function_exists("proc_open")) {
22 Worker::executeIfIdle();
26 Worker::clearProcesses();
28 $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
30 if ($workers[0]["processes"] > Config::get("system", "worker_queues", 4)) {
34 Worker::startProcess();
36 logger("Front end worker started: ".getmypid());
40 if ($r = Worker::workerProcess()) {
41 // On most configurations this parameter wouldn't have any effect.
42 // But since it doesn't destroy anything, we just try to get more execution time in any way.
45 $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
46 $condition = ['id' => $r[0]["id"], 'pid' => 0];
47 if (DBA::update('workerqueue', $fields, $condition)) {
48 Worker::execute($r[0]);
54 Worker::unclaimProcess();
58 logger("Front end worker ended: ".getmypid());