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