]> git.mxchange.org Git - friendica.git/blob - src/Core/Worker/Daemon.php
Renamed variable
[friendica.git] / src / Core / Worker / Daemon.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Worker;
23
24 use Friendica\App\Mode;
25 use Friendica\Core\Logger;
26 use Friendica\DI;
27
28 /**
29  * Contains the class for the worker background job processing
30  */
31 class Daemon
32 {
33         private static $mode = null;
34
35         /**
36          * Checks if the worker is running in the daemon mode.
37          *
38          * @return boolean
39          */
40         public static function isMode()
41         {
42                 if (!is_null(self::$mode)) {
43                         return self::$mode;
44                 }
45
46                 if (DI::mode()->getExecutor() == Mode::DAEMON) {
47                         return true;
48                 }
49
50                 $daemon_mode = DI::config()->get('system', 'worker_daemon_mode', false, true);
51                 if ($daemon_mode) {
52                         return $daemon_mode;
53                 }
54
55                 if (!function_exists('pcntl_fork')) {
56                         self::$mode = false;
57                         return false;
58                 }
59
60                 $pidfile = DI::config()->get('system', 'pidfile');
61                 if (empty($pidfile)) {
62                         // No pid file, no daemon
63                         self::$mode = false;
64                         return false;
65                 }
66
67                 if (!is_readable($pidfile)) {
68                         // No pid file. We assume that the daemon had been intentionally stopped.
69                         self::$mode = false;
70                         return false;
71                 }
72
73                 $pid     = intval(file_get_contents($pidfile));
74                 $running = posix_kill($pid, 0);
75
76                 self::$mode = $running;
77                 return $running;
78         }
79
80         /**
81          * Test if the daemon is running. If not, it will be started
82          *
83          * @return void
84          */
85         public static function checkState()
86         {
87                 if (!DI::config()->get('system', 'daemon_watchdog', false)) {
88                         return;
89                 }
90
91                 if (!DI::mode()->isNormal()) {
92                         return;
93                 }
94
95                 // Check every minute if the daemon is running
96                 if (DI::config()->get('system', 'last_daemon_check', 0) + 60 > time()) {
97                         return;
98                 }
99
100                 DI::config()->set('system', 'last_daemon_check', time());
101
102                 $pidfile = DI::config()->get('system', 'pidfile');
103                 if (empty($pidfile)) {
104                         // No pid file, no daemon
105                         return;
106                 }
107
108                 if (!is_readable($pidfile)) {
109                         // No pid file. We assume that the daemon had been intentionally stopped.
110                         return;
111                 }
112
113                 $pid = intval(file_get_contents($pidfile));
114                 if (posix_kill($pid, 0)) {
115                         Logger::info('Daemon process is running', ['pid' => $pid]);
116                         return;
117                 }
118
119                 Logger::warning('Daemon process is not running', ['pid' => $pid]);
120
121                 self::spawn();
122         }
123
124         /**
125          * Spawn a new daemon process
126          *
127          * @return void
128          */
129         private static function spawn()
130         {
131                 Logger::notice('Starting new daemon process');
132                 DI::system()->run('bin/daemon.php', ['start']);
133                 Logger::notice('New daemon process started');
134         }
135 }