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