3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Worker;
24 use Friendica\App\Mode;
25 use Friendica\Core\Logger;
29 * Contains the class for the worker background job processing
33 private static $mode = null;
36 * Checks if the worker is running in the daemon mode.
40 public static function isMode()
42 if (!is_null(self::$mode)) {
46 if (DI::mode()->getExecutor() == Mode::DAEMON) {
50 $daemon_mode = DI::config()->get('system', 'worker_daemon_mode', false, true);
55 if (!function_exists('pcntl_fork')) {
60 $pidfile = DI::config()->get('system', 'pidfile');
61 if (empty($pidfile)) {
62 // No pid file, no daemon
67 if (!is_readable($pidfile)) {
68 // No pid file. We assume that the daemon had been intentionally stopped.
73 $pid = intval(file_get_contents($pidfile));
74 $running = posix_kill($pid, 0);
76 self::$mode = $running;
81 * Test if the daemon is running. If not, it will be started
85 public static function checkState()
87 if (!DI::config()->get('system', 'daemon_watchdog', false)) {
91 if (!DI::mode()->isNormal()) {
95 // Check every minute if the daemon is running
96 if (DI::config()->get('system', 'last_daemon_check', 0) + 60 > time()) {
100 DI::config()->set('system', 'last_daemon_check', time());
102 $pidfile = DI::config()->get('system', 'pidfile');
103 if (empty($pidfile)) {
104 // No pid file, no daemon
108 if (!is_readable($pidfile)) {
109 // No pid file. We assume that the daemon had been intentionally stopped.
113 $pid = intval(file_get_contents($pidfile));
114 if (posix_kill($pid, 0)) {
115 Logger::info('Daemon process is running', ['pid' => $pid]);
119 Logger::warning('Daemon process is not running', ['pid' => $pid]);
125 * Spawn a new daemon process
129 private static function spawn()
131 Logger::notice('Starting new daemon process');
132 DI::system()->run('bin/daemon.php', ['start']);
133 Logger::notice('New daemon process started');