]> git.mxchange.org Git - friendica.git/blob - src/Module/Worker.php
Merge pull request #9554 from annando/insert-mode
[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\Process;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker as WorkerCore;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Util\DateTimeFormat;
31
32 /**
33  * Module for starting the backend worker through a frontend call
34  */
35 class Worker extends BaseModule
36 {
37         public static function rawContent(array $parameters = [])
38         {
39                 if (!DI::config()->get("system", "frontend_worker")) {
40                         return;
41                 }
42
43                 // Ensure that all "strtotime" operations do run timezone independent
44                 date_default_timezone_set('UTC');
45
46                 // We don't need the following lines if we can execute background jobs.
47                 // So we just wake up the worker if it sleeps.
48                 if (function_exists("proc_open")) {
49                         WorkerCore::executeIfIdle();
50                         return;
51                 }
52
53                 WorkerCore::clearProcesses();
54
55                 $workers = DBA::count('process', ['command' => 'worker.php']);
56
57                 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
58                         return;
59                 }
60
61                 DI::process()->start();
62
63                 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
64
65                 WorkerCore::callWorker();
66
67                 if ($r = WorkerCore::workerProcess()) {
68                         // On most configurations this parameter wouldn't have any effect.
69                         // But since it doesn't destroy anything, we just try to get more execution time in any way.
70                         set_time_limit(0);
71
72                         $fields = ['executed' => DateTimeFormat::utcNow(), 'pid' => getmypid(), 'done' => false];
73                         $condition =  ['id' => $r[0]["id"], 'pid' => 0];
74                         if (DBA::update('workerqueue', $fields, $condition)) {
75                                 WorkerCore::execute($r[0]);
76                         }
77                 }
78
79                 WorkerCore::callWorker();
80
81                 WorkerCore::unclaimProcess();
82
83                 DI::process()->end();
84
85                 System::httpExit(200, 'Frontend worker stopped.');
86         }
87 }