]> git.mxchange.org Git - friendica.git/blob - src/Module/Worker.php
Move mod/worker to src\Module\Worker
[friendica.git] / src / Module / Worker.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\System;
7 use Friendica\Core\Worker as WorkerCore;
8 use Friendica\Database\DBA;
9 use Friendica\DI;
10 use Friendica\Util\DateTimeFormat;
11
12 /**
13  * Module for starting the backend worker through a frontend call
14  */
15 class Worker extends BaseModule
16 {
17         public static function rawContent(array $parameters = [])
18         {
19                 if (!DI::config()->get("system", "frontend_worker")) {
20                         return;
21                 }
22
23                 // Ensure that all "strtotime" operations do run timezone independent
24                 date_default_timezone_set('UTC');
25
26                 // We don't need the following lines if we can execute background jobs.
27                 // So we just wake up the worker if it sleeps.
28                 if (function_exists("proc_open")) {
29                         WorkerCore::executeIfIdle();
30                         return;
31                 }
32
33                 WorkerCore::clearProcesses();
34
35                 $workers = DBA::count('process', ['command' => 'worker.php']);
36
37                 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
38                         return;
39                 }
40
41                 WorkerCore::startProcess();
42
43                 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
44
45                 WorkerCore::callWorker();
46
47                 if ($r = WorkerCore::workerProcess()) {
48                         // On most configurations this parameter wouldn't have any effect.
49                         // But since it doesn't destroy anything, we just try to get more execution time in any way.
50                         set_time_limit(0);
51
52                         $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
53                         $condition =  ['id' => $r[0]["id"], 'pid' => 0];
54                         if (DBA::update('workerqueue', $fields, $condition)) {
55                                 WorkerCore::execute($r[0]);
56                         }
57                 }
58
59                 WorkerCore::callWorker();
60
61                 WorkerCore::unclaimProcess();
62
63                 WorkerCore::endProcess();
64
65                 System::httpExit(200, 'Frontend worker stopped.');
66         }
67 }