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'))) {
45 die('Please set system.pidfile in config/local.ini.php. For example:'."\n".
47 'pidfile = /path/to/daemon.pid'."\n");
50 $pidfile = Config::get('system', 'pidfile');
52 if (in_array("start", $_SERVER["argv"])) {
56 if (in_array("stop", $_SERVER["argv"])) {
60 if (in_array("status", $_SERVER["argv"])) {
64 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
67 die("Please use either 'start', 'stop' or 'status'.\n");
70 if (empty($_SERVER["argv"][0])) {
71 die("Unexpected script behaviour. This message should never occur.\n");
76 if (is_readable($pidfile)) {
77 $pid = intval(file_get_contents($pidfile));
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");
85 if ($mode == "status") {
86 if (posix_kill($pid, 0)) {
87 die("Daemon process $pid is running.\n");
92 Config::set('system', 'worker_daemon_mode', false);
93 die("Daemon process $pid isn't running.\n");
96 if ($mode == "stop") {
97 posix_kill($pid, SIGTERM);
101 Logger::log("Worker daemon process $pid was killed.", Logger::DEBUG);
103 Config::set('system', 'worker_daemon_mode', false);
104 die("Worker daemon process $pid was killed.\n");
107 if (!empty($pid) && posix_kill($pid, 0)) {
108 die("Daemon process $pid is already running.\n");
111 Logger::log('Starting worker daemon.', Logger::DEBUG);
114 echo "Starting worker daemon.\n";
116 // Switch over to daemon mode.
117 if ($pid = pcntl_fork()) {
121 fclose(STDIN); // Close all of the standard
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.
129 register_shutdown_function('shutdown');
131 if (posix_setsid() < 0) {
135 if ($pid = pcntl_fork()) {
140 file_put_contents($pidfile, $pid);
142 // We lose the database connection upon forking
146 Config::set('system', 'worker_daemon_mode', true);
148 // Just to be sure that this script really runs endlessly
151 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
156 // Now running as a daemon.
158 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
159 Logger::log('Forcing cron worker call.', Logger::DEBUG);
163 Worker::spawnWorker($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.
173 Logger::log("Sleeping", Logger::DEBUG);
176 $seconds = (time() - $start);
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);
185 $timeout = ($seconds >= $wait_interval);
186 } while (!$timeout && !Worker::IPCJobsExists());
190 Logger::log("Woke up after $wait_interval seconds.", Logger::DEBUG);
193 Logger::log("Worker jobs are calling to be forked.", Logger::DEBUG);
197 function shutdown() {
198 posix_kill(posix_getpid(), SIGHUP);