]> git.mxchange.org Git - friendica.git/blob - mod/worker.php
Merge branch '2019.03-RC'
[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\Logger;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBA;
11 use Friendica\Util\DateTimeFormat;
12
13 function worker_init()
14 {
15
16         if (!Config::get("system", "frontend_worker")) {
17                 return;
18         }
19
20         // Ensure that all "strtotime" operations do run timezone independent
21         date_default_timezone_set('UTC');
22
23         // We don't need the following lines if we can execute background jobs.
24         // So we just wake up the worker if it sleeps.
25         if (function_exists("proc_open")) {
26                 Worker::executeIfIdle();
27                 return;
28         }
29
30         Worker::clearProcesses();
31
32         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
33
34         if ($workers[0]["processes"] > Config::get("system", "worker_queues", 4)) {
35                 return;
36         }
37
38         Worker::startProcess();
39
40         Logger::log("Front end worker started: ".getmypid());
41
42         Worker::callWorker();
43
44         if ($r = Worker::workerProcess()) {
45                 // On most configurations this parameter wouldn't have any effect.
46                 // But since it doesn't destroy anything, we just try to get more execution time in any way.
47                 set_time_limit(0);
48
49                 $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
50                 $condition =  ['id' => $r[0]["id"], 'pid' => 0];
51                 if (DBA::update('workerqueue', $fields, $condition)) {
52                         Worker::execute($r[0]);
53                 }
54         }
55
56         Worker::callWorker();
57
58         Worker::unclaimProcess();
59
60         Worker::endProcess();
61
62         Logger::log("Front end worker ended: ".getmypid());
63
64         exit();
65 }