]> git.mxchange.org Git - friendica.git/blob - src/Module/Worker.php
Speed improvement
[friendica.git] / src / Module / Worker.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\Core\Worker as WorkerCore;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Util\DateTimeFormat;
30
31 /**
32  * Module for starting the backend worker through a frontend call
33  */
34 class Worker extends BaseModule
35 {
36         public static function rawContent(array $parameters = [])
37         {
38                 if (!DI::config()->get("system", "frontend_worker")) {
39                         return;
40                 }
41
42                 // Ensure that all "strtotime" operations do run timezone independent
43                 date_default_timezone_set('UTC');
44
45                 // We don't need the following lines if we can execute background jobs.
46                 // So we just wake up the worker if it sleeps.
47                 if (function_exists("proc_open")) {
48                         WorkerCore::executeIfIdle();
49                         return;
50                 }
51
52                 WorkerCore::clearProcesses();
53
54                 $workers = DBA::count('process', ['command' => 'worker.php']);
55
56                 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
57                         return;
58                 }
59
60                 WorkerCore::startProcess();
61
62                 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
63
64                 WorkerCore::callWorker();
65
66                 if ($r = WorkerCore::workerProcess()) {
67                         // On most configurations this parameter wouldn't have any effect.
68                         // But since it doesn't destroy anything, we just try to get more execution time in any way.
69                         set_time_limit(0);
70
71                         $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
72                         $condition =  ['id' => $r[0]["id"], 'pid' => 0];
73                         if (DBA::update('workerqueue', $fields, $condition)) {
74                                 WorkerCore::execute($r[0]);
75                         }
76                 }
77
78                 WorkerCore::callWorker();
79
80                 WorkerCore::unclaimProcess();
81
82                 WorkerCore::endProcess();
83
84                 System::httpExit(200, 'Frontend worker stopped.');
85         }
86 }