]> git.mxchange.org Git - friendica.git/blob - mod/worker.php
Add missing Friendica\DI namespace to files (PHPStorm inspection)
[friendica.git] / mod / worker.php
1 <?php
2 /**
3  * @file mod/worker.php
4  * 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\DI;
12 use Friendica\Util\DateTimeFormat;
13
14 function worker_init()
15 {
16
17         if (!DI::config()->get("system", "frontend_worker")) {
18                 return;
19         }
20
21         // Ensure that all "strtotime" operations do run timezone independent
22         date_default_timezone_set('UTC');
23
24         // We don't need the following lines if we can execute background jobs.
25         // So we just wake up the worker if it sleeps.
26         if (function_exists("proc_open")) {
27                 Worker::executeIfIdle();
28                 return;
29         }
30
31         Worker::clearProcesses();
32
33         $workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'worker.php'");
34
35         if ($workers[0]["processes"] > DI::config()->get("system", "worker_queues", 4)) {
36                 return;
37         }
38
39         Worker::startProcess();
40
41         Logger::log("Front end worker started: ".getmypid());
42
43         Worker::callWorker();
44
45         if ($r = Worker::workerProcess()) {
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 }