5 * @brief Run the worker from a daemon.
7 * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
11 use Friendica\Core\Config;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
18 $longopts = ['foreground'];
19 $options = getopt($shortopts, $longopts);
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]);
25 if (substr($directory, 0, 1) != "/") {
26 $directory = $_SERVER["PWD"] . "/" . $directory;
28 $directory = realpath($directory . "/..");
33 require_once "boot.php";
34 require_once "include/dba.php";
36 $a = new App(dirname(__DIR__));
38 if ($a->getMode()->isInstall()) {
39 die("Friendica isn't properly installed yet.\n");
44 if (empty(Config::get('system', 'pidfile'))) {
46 Please set system.pidfile in config/local.config.php. For example:
49 'pidfile' => '/path/to/daemon.pid',
55 $pidfile = Config::get('system', 'pidfile');
57 if (in_array("start", $_SERVER["argv"])) {
61 if (in_array("stop", $_SERVER["argv"])) {
65 if (in_array("status", $_SERVER["argv"])) {
69 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
72 die("Please use either 'start', 'stop' or 'status'.\n");
75 if (empty($_SERVER["argv"][0])) {
76 die("Unexpected script behaviour. This message should never occur.\n");
81 if (is_readable($pidfile)) {
82 $pid = intval(file_get_contents($pidfile));
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");
90 if ($mode == "status") {
91 if (posix_kill($pid, 0)) {
92 die("Daemon process $pid is running.\n");
97 Config::set('system', 'worker_daemon_mode', false);
98 die("Daemon process $pid isn't running.\n");
101 if ($mode == "stop") {
102 posix_kill($pid, SIGTERM);
106 Logger::log("Worker daemon process $pid was killed.", Logger::DEBUG);
108 Config::set('system', 'worker_daemon_mode', false);
109 die("Worker daemon process $pid was killed.\n");
112 if (!empty($pid) && posix_kill($pid, 0)) {
113 die("Daemon process $pid is already running.\n");
116 Logger::log('Starting worker daemon.', Logger::DEBUG);
119 echo "Starting worker daemon.\n";
121 // Switch over to daemon mode.
122 if ($pid = pcntl_fork()) {
126 fclose(STDIN); // Close all of the standard
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.
134 register_shutdown_function('shutdown');
136 if (posix_setsid() < 0) {
140 if ($pid = pcntl_fork()) {
145 file_put_contents($pidfile, $pid);
147 // We lose the database connection upon forking
151 Config::set('system', 'worker_daemon_mode', true);
153 // Just to be sure that this script really runs endlessly
156 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
161 // Now running as a daemon.
163 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
164 Logger::log('Forcing cron worker call.', Logger::DEBUG);
168 Worker::spawnWorker($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.
178 Logger::log("Sleeping", Logger::DEBUG);
181 $seconds = (time() - $start);
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);
190 $timeout = ($seconds >= $wait_interval);
191 } while (!$timeout && !Worker::IPCJobsExists());
195 Logger::log("Woke up after $wait_interval seconds.", Logger::DEBUG);
198 Logger::log("Worker jobs are calling to be forked.", Logger::DEBUG);
202 function shutdown() {
203 posix_kill(posix_getpid(), SIGHUP);