]> git.mxchange.org Git - friendica.git/blob - mod/worker.php
Merge pull request #6616 from m4sk1n/patch-1
[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         // We don't need the following lines if we can execute background jobs.
21         // So we just wake up the worker if it sleeps.
22         if (function_exists("proc_open")) {
23                 Worker::executeIfIdle();
24                 return;
25         }
26
27         Worker::clearProcesses();
28
29         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
30
31         if ($workers[0]["processes"] > Config::get("system", "worker_queues", 4)) {
32                 return;
33         }
34
35         Worker::startProcess();
36
37         Logger::log("Front end worker started: ".getmypid());
38
39         Worker::callWorker();
40
41         $passing_slow = false;
42         $entries = 0;
43         $deferred = 0;
44
45         if ($r = Worker::workerProcess($passing_slow, $entries, $deferred)) {
46                 // On most configurations this parameter wouldn't have any effect.
47                 // But since it doesn't destroy anything, we just try to get more execution time in any way.
48                 set_time_limit(0);
49
50                 $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
51                 $condition =  ['id' => $r[0]["id"], 'pid' => 0];
52                 if (DBA::update('workerqueue', $fields, $condition)) {
53                         Worker::execute($r[0]);
54                 }
55         }
56
57         Worker::callWorker();
58
59         Worker::unclaimProcess();
60
61         Worker::endProcess();
62
63         Logger::log("Front end worker ended: ".getmypid());
64
65         exit();
66 }