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