]> git.mxchange.org Git - friendica.git/blob - mod/worker.php
Misc cleanups (#5417)
[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\Util\DateTimeFormat;
10
11 function worker_init()
12 {
13
14         if (!Config::get("system", "frontend_worker")) {
15                 return;
16         }
17
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();
22                 return;
23         }
24
25         Worker::clearProcesses();
26
27         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
28
29         if ($workers[0]["processes"] > Config::get("system", "worker_queues", 4)) {
30                 return;
31         }
32
33         Worker::startProcess();
34
35         logger("Front end worker started: ".getmypid());
36
37         Worker::callWorker();
38
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.
42                 set_time_limit(0);
43
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]);
48                 }
49         }
50
51         Worker::callWorker();
52
53         Worker::unclaimProcess();
54
55         Worker::endProcess();
56
57         logger("Front end worker ended: ".getmypid());
58
59         killme();
60 }