]> git.mxchange.org Git - friendica.git/blob - mod/worker.php
Correct content type
[friendica.git] / mod / worker.php
1 <?php
2 /**
3  * @file mod/worker.php
4  * @brief Module for running the worker as frontend process
5  */
6
7 use Friendica\Core\Config;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBA;
10 use Friendica\Util\DateTimeFormat;
11
12 function worker_init()
13 {
14
15         if (!Config::get("system", "frontend_worker")) {
16                 return;
17         }
18
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();
23                 return;
24         }
25
26         Worker::clearProcesses();
27
28         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
29
30         if ($workers[0]["processes"] > Config::get("system", "worker_queues", 4)) {
31                 return;
32         }
33
34         Worker::startProcess();
35
36         logger("Front end worker started: ".getmypid());
37
38         Worker::callWorker();
39
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.
43                 set_time_limit(0);
44
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]);
49                 }
50         }
51
52         Worker::callWorker();
53
54         Worker::unclaimProcess();
55
56         Worker::endProcess();
57
58         logger("Front end worker ended: ".getmypid());
59
60         killme();
61 }