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\Process;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker as WorkerCore;
28 use Friendica\Database\DBA;
30 use Friendica\Util\DateTimeFormat;
33 * Module for starting the backend worker through a frontend call
35 class Worker extends BaseModule
37 public static function rawContent(array $parameters = [])
39 if (!DI::config()->get("system", "frontend_worker")) {
43 // Ensure that all "strtotime" operations do run timezone independent
44 date_default_timezone_set('UTC');
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();
53 WorkerCore::clearProcesses();
55 $workers = DBA::count('process', ['command' => 'worker.php']);
57 if ($workers > DI::config()->get("system", "worker_queues", 4)) {
61 DI::process()->start();
63 DI::logger()->notice('Front end worker started.', ['pid' => getmypid()]);
65 WorkerCore::callWorker();
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.
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]);
79 WorkerCore::callWorker();
81 WorkerCore::unclaimProcess();
85 System::httpExit(200, 'Frontend worker stopped.');