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;
15 use Psr\Log\LoggerInterface;
19 $longopts = ['foreground'];
20 $options = getopt($shortopts, $longopts);
22 // Ensure that daemon.php is executed from the base path of the installation
23 if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
24 $directory = dirname($_SERVER["argv"][0]);
26 if (substr($directory, 0, 1) != "/") {
27 $directory = $_SERVER["PWD"] . "/" . $directory;
29 $directory = realpath($directory . "/..");
34 require dirname(__DIR__) . '/vendor/autoload.php';
36 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
37 $dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['daemon']]);
39 \Friendica\BaseObject::setDependencyInjection($dice);
40 $a = \Friendica\BaseObject::getApp();
42 if ($a->getMode()->isInstall()) {
43 die("Friendica isn't properly installed yet.\n");
48 if (empty(Config::get('system', 'pidfile'))) {
50 Please set system.pidfile in config/local.config.php. For example:
53 'pidfile' => '/path/to/daemon.pid',
59 $pidfile = Config::get('system', 'pidfile');
61 if (in_array("start", $_SERVER["argv"])) {
65 if (in_array("stop", $_SERVER["argv"])) {
69 if (in_array("status", $_SERVER["argv"])) {
73 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
76 die("Please use either 'start', 'stop' or 'status'.\n");
79 if (empty($_SERVER["argv"][0])) {
80 die("Unexpected script behaviour. This message should never occur.\n");
85 if (is_readable($pidfile)) {
86 $pid = intval(file_get_contents($pidfile));
89 if (empty($pid) && in_array($mode, ["stop", "status"])) {
90 Config::set('system', 'worker_daemon_mode', false);
91 die("Pidfile wasn't found. Is the daemon running?\n");
94 if ($mode == "status") {
95 if (posix_kill($pid, 0)) {
96 die("Daemon process $pid is running.\n");
101 Config::set('system', 'worker_daemon_mode', false);
102 die("Daemon process $pid isn't running.\n");
105 if ($mode == "stop") {
106 posix_kill($pid, SIGTERM);
110 Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
112 Config::set('system', 'worker_daemon_mode', false);
113 die("Worker daemon process $pid was killed.\n");
116 if (!empty($pid) && posix_kill($pid, 0)) {
117 die("Daemon process $pid is already running.\n");
120 Logger::notice('Starting worker daemon.', ["pid" => $pid]);
123 echo "Starting worker daemon.\n";
125 // Switch over to daemon mode.
126 if ($pid = pcntl_fork()) {
130 fclose(STDIN); // Close all of the standard
132 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
133 // fclose(STDOUT); // file descriptors as we
134 // fclose(STDERR); // are running as a daemon.
138 register_shutdown_function('shutdown');
140 if (posix_setsid() < 0) {
144 if ($pid = pcntl_fork()) {
149 file_put_contents($pidfile, $pid);
151 // We lose the database connection upon forking
155 Config::set('system', 'worker_daemon_mode', true);
157 // Just to be sure that this script really runs endlessly
160 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
165 // Now running as a daemon.
167 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
168 Logger::info('Forcing cron worker call.', ["pid" => $pid]);
172 Worker::spawnWorker($do_cron);
175 // We force a reconnect of the database connection.
176 // This is done to ensure that the connection don't get lost over time.
182 Logger::info("Sleeping", ["pid" => $pid]);
185 $seconds = (time() - $start);
187 // logarithmic wait time calculation.
188 // Background: After jobs had been started, they often fork many workers.
189 // To not waste too much time, the sleep period increases.
190 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
191 $sleep = round(log10($arg) * 1000000, 0);
194 $timeout = ($seconds >= $wait_interval);
195 } while (!$timeout && !Worker::IPCJobsExists());
199 Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
202 Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
206 function shutdown() {
207 posix_kill(posix_getpid(), SIGHUP);