5 * @brief Run the worker from a daemon.
7 * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
10 use Friendica\Core\Config;
11 use Friendica\Core\Logger;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Factory;
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 dirname(__DIR__) . '/vendor/autoload.php';
35 $dice = new \Dice\Dice();
36 $dice = $dice->addRules(include __DIR__ . '/../static/dependencies.config.php');
38 $a = Factory\DependencyFactory::setUp('daemon', $dice);
40 if ($a->getMode()->isInstall()) {
41 die("Friendica isn't properly installed yet.\n");
46 if (empty(Config::get('system', 'pidfile'))) {
48 Please set system.pidfile in config/local.config.php. For example:
51 'pidfile' => '/path/to/daemon.pid',
57 $pidfile = Config::get('system', 'pidfile');
59 if (in_array("start", $_SERVER["argv"])) {
63 if (in_array("stop", $_SERVER["argv"])) {
67 if (in_array("status", $_SERVER["argv"])) {
71 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
74 die("Please use either 'start', 'stop' or 'status'.\n");
77 if (empty($_SERVER["argv"][0])) {
78 die("Unexpected script behaviour. This message should never occur.\n");
83 if (is_readable($pidfile)) {
84 $pid = intval(file_get_contents($pidfile));
87 if (empty($pid) && in_array($mode, ["stop", "status"])) {
88 Config::set('system', 'worker_daemon_mode', false);
89 die("Pidfile wasn't found. Is the daemon running?\n");
92 if ($mode == "status") {
93 if (posix_kill($pid, 0)) {
94 die("Daemon process $pid is running.\n");
99 Config::set('system', 'worker_daemon_mode', false);
100 die("Daemon process $pid isn't running.\n");
103 if ($mode == "stop") {
104 posix_kill($pid, SIGTERM);
108 Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
110 Config::set('system', 'worker_daemon_mode', false);
111 die("Worker daemon process $pid was killed.\n");
114 if (!empty($pid) && posix_kill($pid, 0)) {
115 die("Daemon process $pid is already running.\n");
118 Logger::notice('Starting worker daemon.', ["pid" => $pid]);
121 echo "Starting worker daemon.\n";
123 // Switch over to daemon mode.
124 if ($pid = pcntl_fork()) {
128 fclose(STDIN); // Close all of the standard
130 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
131 // fclose(STDOUT); // file descriptors as we
132 // fclose(STDERR); // are running as a daemon.
136 register_shutdown_function('shutdown');
138 if (posix_setsid() < 0) {
142 if ($pid = pcntl_fork()) {
147 file_put_contents($pidfile, $pid);
149 // We lose the database connection upon forking
150 $a->getDatabase()->reconnect();
153 Config::set('system', 'worker_daemon_mode', true);
155 // Just to be sure that this script really runs endlessly
158 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
163 // Now running as a daemon.
165 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
166 Logger::info('Forcing cron worker call.', ["pid" => $pid]);
170 Worker::spawnWorker($do_cron);
173 // We force a reconnect of the database connection.
174 // This is done to ensure that the connection don't get lost over time.
180 Logger::info("Sleeping", ["pid" => $pid]);
183 $seconds = (time() - $start);
185 // logarithmic wait time calculation.
186 // Background: After jobs had been started, they often fork many workers.
187 // To not waste too much time, the sleep period increases.
188 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
189 $sleep = round(log10($arg) * 1000000, 0);
192 $timeout = ($seconds >= $wait_interval);
193 } while (!$timeout && !Worker::IPCJobsExists());
197 Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
200 Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
204 function shutdown() {
205 posix_kill(posix_getpid(), SIGHUP);