]> git.mxchange.org Git - friendica.git/blob - bin/daemon.php
Fixed E_NOTICE when no valid result has been returned. (#5457)
[friendica.git] / bin / daemon.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * @file bin/daemon.php
5  * @brief Run the worker from a daemon.
6  *
7  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
8  */
9
10 use Friendica\App;
11 use Friendica\Core\Config;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14
15 // Ensure that daemon.php is executed from the base path of the installation
16 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
17         $directory = dirname($_SERVER["argv"][0]);
18
19         if (substr($directory, 0, 1) != "/") {
20                 $directory = $_SERVER["PWD"] . "/" . $directory;
21         }
22         $directory = realpath($directory . "/..");
23
24         chdir($directory);
25 }
26
27 require_once "boot.php";
28 require_once "include/dba.php";
29
30 $a = new App(dirname(__DIR__));
31
32 if ($a->isInstallMode()) {
33         die("Friendica isn't properly installed yet.\n");
34 }
35
36 Config::load();
37
38 if (empty(Config::get('system', 'pidfile'))) {
39         die('Please set system.pidfile in config/local.ini.php. For example:'."\n".
40                 '[system]'."\n".
41                 'pidfile = /path/to/daemon.pid'."\n");
42 }
43
44 $pidfile = Config::get('system', 'pidfile');
45
46 if (in_array("start", $_SERVER["argv"])) {
47         $mode = "start";
48 }
49
50 if (in_array("stop", $_SERVER["argv"])) {
51         $mode = "stop";
52 }
53
54 if (in_array("status", $_SERVER["argv"])) {
55         $mode = "status";
56 }
57
58 $foreground = in_array("--foreground", $_SERVER["argv"]);
59
60 if (!isset($mode)) {
61         die("Please use either 'start', 'stop' or 'status'.\n");
62 }
63
64 if (empty($_SERVER["argv"][0])) {
65         die("Unexpected script behaviour. This message should never occur.\n");
66 }
67
68 $pid = null;
69
70 if (is_readable($pidfile)) {
71         $pid = intval(file_get_contents($pidfile));
72 }
73
74 if (empty($pid) && in_array($mode, ["stop", "status"])) {
75         Config::set('system', 'worker_daemon_mode', false);
76         die("Pidfile wasn't found. Is the daemon running?\n");
77 }
78
79 if ($mode == "status") {
80         if (posix_kill($pid, 0)) {
81                 die("Daemon process $pid is running.\n");
82         }
83
84         unlink($pidfile);
85
86         Config::set('system', 'worker_daemon_mode', false);
87         die("Daemon process $pid isn't running.\n");
88 }
89
90 if ($mode == "stop") {
91         posix_kill($pid, SIGTERM);
92
93         unlink($pidfile);
94
95         logger("Worker daemon process $pid was killed.", LOGGER_DEBUG);
96
97         Config::set('system', 'worker_daemon_mode', false);
98         die("Worker daemon process $pid was killed.\n");
99 }
100
101 if (!empty($pid) && posix_kill($pid, 0)) {
102         die("Daemon process $pid is already running.\n");
103 }
104
105 logger('Starting worker daemon.', LOGGER_DEBUG);
106
107 if (!$foreground) {
108         echo "Starting worker daemon.\n";
109
110         // Switch over to daemon mode.
111         if ($pid = pcntl_fork()) {
112                 return;     // Parent
113         }
114
115         fclose(STDIN);  // Close all of the standard
116
117         // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
118         // fclose(STDOUT); // file descriptors as we
119         // fclose(STDERR); // are running as a daemon.
120
121         DBA::disconnect();
122
123         register_shutdown_function('shutdown');
124
125         if (posix_setsid() < 0) {
126                 return;
127         }
128
129         if ($pid = pcntl_fork()) {
130                 return;     // Parent
131         }
132
133         $pid = getmypid();
134         file_put_contents($pidfile, $pid);
135
136         // We lose the database connection upon forking
137         $a->loadDatabase();
138 }
139
140 Config::set('system', 'worker_daemon_mode', true);
141
142 // Just to be sure that this script really runs endlessly
143 set_time_limit(0);
144
145 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
146
147 $do_cron = true;
148 $last_cron = 0;
149
150 // Now running as a daemon.
151 while (true) {
152         if (!$do_cron && ($last_cron + $wait_interval) < time()) {
153                 logger('Forcing cron worker call.', LOGGER_DEBUG);
154                 $do_cron = true;
155         }
156
157         Worker::spawnWorker($do_cron);
158
159         if ($do_cron) {
160                 // We force a reconnect of the database connection.
161                 // This is done to ensure that the connection don't get lost over time.
162                 DBA::reconnect();
163
164                 $last_cron = time();
165         }
166
167         logger("Sleeping", LOGGER_DEBUG);
168         $start = time();
169         do {
170                 $seconds = (time() - $start);
171
172                 // logarithmic wait time calculation.
173                 // Background: After jobs had been started, they often fork many workers.
174                 // To not waste too much time, the sleep period increases.
175                 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
176                 $sleep = round(log10($arg) * 1000000, 0);
177                 usleep($sleep);
178
179                 $timeout = ($seconds >= $wait_interval);
180         } while (!$timeout && !Worker::IPCJobsExists());
181
182         if ($timeout) {
183                 $do_cron = true;
184                 logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG);
185         } else {
186                 $do_cron = false;
187                 logger("Worker jobs are calling to be forked.", LOGGER_DEBUG);
188         }
189 }
190
191 function shutdown() {
192         posix_kill(posix_getpid(), SIGHUP);
193 }