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;
14 use Friendica\Util\LoggerFactory;
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 $logger = LoggerFactory::create('daemon');
37 $a = new App(dirname(__DIR__), $logger);
39 if ($a->getMode()->isInstall()) {
40 die("Friendica isn't properly installed yet.\n");
45 if (empty(Config::get('system', 'pidfile'))) {
47 Please set system.pidfile in config/local.config.php. For example:
50 'pidfile' => '/path/to/daemon.pid',
56 $pidfile = Config::get('system', 'pidfile');
58 if (in_array("start", $_SERVER["argv"])) {
62 if (in_array("stop", $_SERVER["argv"])) {
66 if (in_array("status", $_SERVER["argv"])) {
70 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
73 die("Please use either 'start', 'stop' or 'status'.\n");
76 if (empty($_SERVER["argv"][0])) {
77 die("Unexpected script behaviour. This message should never occur.\n");
82 if (is_readable($pidfile)) {
83 $pid = intval(file_get_contents($pidfile));
86 if (empty($pid) && in_array($mode, ["stop", "status"])) {
87 Config::set('system', 'worker_daemon_mode', false);
88 die("Pidfile wasn't found. Is the daemon running?\n");
91 if ($mode == "status") {
92 if (posix_kill($pid, 0)) {
93 die("Daemon process $pid is running.\n");
98 Config::set('system', 'worker_daemon_mode', false);
99 die("Daemon process $pid isn't running.\n");
102 if ($mode == "stop") {
103 posix_kill($pid, SIGTERM);
107 $logger->notice("Worker daemon process was killed", ["pid" => $pid]);
109 Config::set('system', 'worker_daemon_mode', false);
110 die("Worker daemon process $pid was killed.\n");
113 if (!empty($pid) && posix_kill($pid, 0)) {
114 die("Daemon process $pid is already running.\n");
117 $logger->notice('Starting worker daemon.', ["pid" => $pid]);
120 echo "Starting worker daemon.\n";
122 // Switch over to daemon mode.
123 if ($pid = pcntl_fork()) {
127 fclose(STDIN); // Close all of the standard
129 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
130 // fclose(STDOUT); // file descriptors as we
131 // fclose(STDERR); // are running as a daemon.
135 register_shutdown_function('shutdown');
137 if (posix_setsid() < 0) {
141 if ($pid = pcntl_fork()) {
146 file_put_contents($pidfile, $pid);
148 // We lose the database connection upon forking
152 Config::set('system', 'worker_daemon_mode', true);
154 // Just to be sure that this script really runs endlessly
157 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
162 // Now running as a daemon.
164 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
165 $logger->info('Forcing cron worker call.', ["pid" => $pid]);
169 Worker::spawnWorker($do_cron);
172 // We force a reconnect of the database connection.
173 // This is done to ensure that the connection don't get lost over time.
179 $logger->info("Sleeping", ["pid" => $pid]);
182 $seconds = (time() - $start);
184 // logarithmic wait time calculation.
185 // Background: After jobs had been started, they often fork many workers.
186 // To not waste too much time, the sleep period increases.
187 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
188 $sleep = round(log10($arg) * 1000000, 0);
191 $timeout = ($seconds >= $wait_interval);
192 } while (!$timeout && !Worker::IPCJobsExists());
196 $logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
199 $logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
203 function shutdown() {
204 posix_kill(posix_getpid(), SIGHUP);