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\BaseObject;
12 use Friendica\Core\Config;
13 use Friendica\Core\Worker;
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__));
31 BaseObject::setApp($a);
33 require_once ".htconfig.php";
34 dba::connect($db_host, $db_user, $db_pass, $db_data);
38 if (!isset($pidfile)) {
39 die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
40 '$pidfile = "/path/to/daemon.pid";'."\n");
43 if (in_array("start", $_SERVER["argv"])) {
47 if (in_array("stop", $_SERVER["argv"])) {
51 if (in_array("status", $_SERVER["argv"])) {
55 $foreground = in_array("--foreground", $_SERVER["argv"]);
58 die("Please use either 'start', 'stop' or 'status'.\n");
61 if (empty($_SERVER["argv"][0])) {
62 die("Unexpected script behaviour. This message should never occur.\n");
65 $pid = @file_get_contents($pidfile);
67 if (empty($pid) && in_array($mode, ["stop", "status"])) {
68 Config::set('system', 'worker_daemon_mode', false);
69 die("Pidfile wasn't found. Is the daemon running?\n");
72 if ($mode == "status") {
73 if (posix_kill($pid, 0)) {
74 die("Daemon process $pid is running.\n");
79 Config::set('system', 'worker_daemon_mode', false);
80 die("Daemon process $pid isn't running.\n");
83 if ($mode == "stop") {
84 posix_kill($pid, SIGTERM);
88 logger("Worker daemon process $pid was killed.", LOGGER_DEBUG);
90 Config::set('system', 'worker_daemon_mode', false);
91 die("Worker daemon process $pid was killed.\n");
94 if (!empty($pid) && posix_kill($pid, 0)) {
95 die("Daemon process $pid is already running.\n");
98 logger('Starting worker daemon.', LOGGER_DEBUG);
101 echo "Starting worker daemon.\n";
103 // Switch over to daemon mode.
104 if ($pid = pcntl_fork()) {
108 fclose(STDIN); // Close all of the standard
109 fclose(STDOUT); // file descriptors as we
110 fclose(STDERR); // are running as a daemon.
114 register_shutdown_function('shutdown');
116 if (posix_setsid() < 0) {
120 if ($pid = pcntl_fork()) {
125 file_put_contents($pidfile, $pid);
127 // We lose the database connection upon forking
128 dba::connect($db_host, $db_user, $db_pass, $db_data);
131 unset($db_host, $db_user, $db_pass, $db_data);
133 Config::set('system', 'worker_daemon_mode', true);
135 // Just to be sure that this script really runs endlessly
138 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
143 // Now running as a daemon.
145 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
146 logger('Forcing cron worker call.', LOGGER_DEBUG);
150 Worker::spawnWorker($do_cron);
153 // We force a reconnect of the database connection.
154 // This is done to ensure that the connection don't get lost over time.
160 logger("Sleeping", LOGGER_DEBUG);
163 $seconds = (time() - $start);
165 // logarithmic wait time calculation.
166 // Background: After jobs had been started, they often fork many workers.
167 // To not waste too much time, the sleep period increases.
168 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
169 $sleep = round(log10($arg) * 1000000, 0);
172 $timeout = ($seconds >= $wait_interval);
173 } while (!$timeout && !Worker::IPCJobsExists());
177 logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG);
180 logger("Worker jobs are calling to be forked.", LOGGER_DEBUG);
184 function shutdown() {
185 posix_kill(posix_getpid(), SIGHUP);