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\Worker;
13 use Friendica\Database\DBA;
15 // Ensure that daemon.php is executed from the base path of the installation
16 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
17 $directory = dirname($_SERVER["argv"][0]);
19 if (substr($directory, 0, 1) != "/") {
20 $directory = $_SERVER["PWD"]."/".$directory;
22 $directory = realpath($directory."/..");
27 require_once "boot.php";
28 require_once "include/dba.php";
30 $a = new App(dirname(__DIR__));
32 if ($a->isInstallMode()) {
33 die("Friendica isn't properly installed yet.\n");
38 if (empty(Config::get('system', 'pidfile'))) {
39 die('Please set system.pidfile in config/local.ini.php. For example:'."\n".
41 'pidfile = /path/to/daemon.pid'."\n");
44 $pidfile = Config::get('system', 'pidfile');
46 if (in_array("start", $_SERVER["argv"])) {
50 if (in_array("stop", $_SERVER["argv"])) {
54 if (in_array("status", $_SERVER["argv"])) {
58 $foreground = in_array("--foreground", $_SERVER["argv"]);
61 die("Please use either 'start', 'stop' or 'status'.\n");
64 if (empty($_SERVER["argv"][0])) {
65 die("Unexpected script behaviour. This message should never occur.\n");
68 $pid = @file_get_contents($pidfile);
70 if (empty($pid) && in_array($mode, ["stop", "status"])) {
71 Config::set('system', 'worker_daemon_mode', false);
72 die("Pidfile wasn't found. Is the daemon running?\n");
75 if ($mode == "status") {
76 if (posix_kill($pid, 0)) {
77 die("Daemon process $pid is running.\n");
82 Config::set('system', 'worker_daemon_mode', false);
83 die("Daemon process $pid isn't running.\n");
86 if ($mode == "stop") {
87 posix_kill($pid, SIGTERM);
91 logger("Worker daemon process $pid was killed.", LOGGER_DEBUG);
93 Config::set('system', 'worker_daemon_mode', false);
94 die("Worker daemon process $pid was killed.\n");
97 if (!empty($pid) && posix_kill($pid, 0)) {
98 die("Daemon process $pid is already running.\n");
101 logger('Starting worker daemon.', LOGGER_DEBUG);
104 echo "Starting worker daemon.\n";
106 // Switch over to daemon mode.
107 if ($pid = pcntl_fork()) {
111 fclose(STDIN); // Close all of the standard
113 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
114 // fclose(STDOUT); // file descriptors as we
115 // fclose(STDERR); // are running as a daemon.
119 register_shutdown_function('shutdown');
121 if (posix_setsid() < 0) {
125 if ($pid = pcntl_fork()) {
130 file_put_contents($pidfile, $pid);
132 // We lose the database connection upon forking
136 Config::set('system', 'worker_daemon_mode', true);
138 // Just to be sure that this script really runs endlessly
141 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
146 // Now running as a daemon.
148 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
149 logger('Forcing cron worker call.', LOGGER_DEBUG);
153 Worker::spawnWorker($do_cron);
156 // We force a reconnect of the database connection.
157 // This is done to ensure that the connection don't get lost over time.
163 logger("Sleeping", LOGGER_DEBUG);
166 $seconds = (time() - $start);
168 // logarithmic wait time calculation.
169 // Background: After jobs had been started, they often fork many workers.
170 // To not waste too much time, the sleep period increases.
171 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
172 $sleep = round(log10($arg) * 1000000, 0);
175 $timeout = ($seconds >= $wait_interval);
176 } while (!$timeout && !Worker::IPCJobsExists());
180 logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG);
183 logger("Worker jobs are calling to be forked.", LOGGER_DEBUG);
187 function shutdown() {
188 posix_kill(posix_getpid(), SIGHUP);