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