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;
16 use Psr\Log\LoggerInterface;
20 $longopts = ['foreground'];
21 $options = getopt($shortopts, $longopts);
23 // Ensure that daemon.php is executed from the base path of the installation
24 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
25 $directory = dirname($_SERVER["argv"][0]);
27 if (substr($directory, 0, 1) != "/") {
28 $directory = $_SERVER["PWD"] . "/" . $directory;
30 $directory = realpath($directory . "/..");
35 require dirname(__DIR__) . '/vendor/autoload.php';
37 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
38 $dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['daemon']]);
43 if (DI::mode()->isInstall()) {
44 die("Friendica isn't properly installed yet.\n");
49 if (empty(Config::get('system', 'pidfile'))) {
51 Please set system.pidfile in config/local.config.php. For example:
54 'pidfile' => '/path/to/daemon.pid',
60 $pidfile = Config::get('system', 'pidfile');
62 if (in_array("start", $_SERVER["argv"])) {
66 if (in_array("stop", $_SERVER["argv"])) {
70 if (in_array("status", $_SERVER["argv"])) {
74 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
77 die("Please use either 'start', 'stop' or 'status'.\n");
80 if (empty($_SERVER["argv"][0])) {
81 die("Unexpected script behaviour. This message should never occur.\n");
86 if (is_readable($pidfile)) {
87 $pid = intval(file_get_contents($pidfile));
90 if (empty($pid) && in_array($mode, ["stop", "status"])) {
91 Config::set('system', 'worker_daemon_mode', false);
92 die("Pidfile wasn't found. Is the daemon running?\n");
95 if ($mode == "status") {
96 if (posix_kill($pid, 0)) {
97 die("Daemon process $pid is running.\n");
102 Config::set('system', 'worker_daemon_mode', false);
103 die("Daemon process $pid isn't running.\n");
106 if ($mode == "stop") {
107 posix_kill($pid, SIGTERM);
111 Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
113 Config::set('system', 'worker_daemon_mode', false);
114 die("Worker daemon process $pid was killed.\n");
117 if (!empty($pid) && posix_kill($pid, 0)) {
118 die("Daemon process $pid is already running.\n");
121 Logger::notice('Starting worker daemon.', ["pid" => $pid]);
124 echo "Starting worker daemon.\n";
126 // Switch over to daemon mode.
127 if ($pid = pcntl_fork()) {
131 fclose(STDIN); // Close all of the standard
133 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
134 // fclose(STDOUT); // file descriptors as we
135 // fclose(STDERR); // are running as a daemon.
139 register_shutdown_function('shutdown');
141 if (posix_setsid() < 0) {
145 if ($pid = pcntl_fork()) {
150 file_put_contents($pidfile, $pid);
152 // We lose the database connection upon forking
156 Config::set('system', 'worker_daemon_mode', true);
158 // Just to be sure that this script really runs endlessly
161 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
166 // Now running as a daemon.
168 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
169 Logger::info('Forcing cron worker call.', ["pid" => $pid]);
173 Worker::spawnWorker($do_cron);
176 // We force a reconnect of the database connection.
177 // This is done to ensure that the connection don't get lost over time.
183 Logger::info("Sleeping", ["pid" => $pid]);
186 $seconds = (time() - $start);
188 // logarithmic wait time calculation.
189 // Background: After jobs had been started, they often fork many workers.
190 // To not waste too much time, the sleep period increases.
191 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
192 $sleep = round(log10($arg) * 1000000, 0);
195 $timeout = ($seconds >= $wait_interval);
196 } while (!$timeout && !Worker::IPCJobsExists());
200 Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
203 Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
207 function shutdown() {
208 posix_kill(posix_getpid(), SIGHUP);