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