3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\Core\Worker as WorkerCore;
27 use Friendica\Database\DBA;
29 use Friendica\Util\DateTimeFormat;
32 * Module for starting the backend worker through a frontend call
34 class Worker extends BaseModule
36 public static function rawContent(array $parameters = [])
38 if (!DI::config()->get("system", "frontend_worker")) {
42 // Ensure that all "strtotime" operations do run timezone independent
43 date_default_timezone_set('UTC');
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();
52 WorkerCore::clearProcesses();
54 $workers = DBA::count('process', ['command' => 'worker.php']);
56 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
60 WorkerCore::startProcess();
62 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
64 WorkerCore::callWorker();
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.
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]);
78 WorkerCore::callWorker();
80 WorkerCore::unclaimProcess();
82 WorkerCore::endProcess();
84 System::httpExit(200, 'Frontend worker stopped.');