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\Config\Cache;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15 use Friendica\Factory;
16 use Friendica\Util\BasePath;
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 $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
38 $configLoader = new Cache\ConfigCacheLoader($basedir);
39 $configCache = Factory\ConfigFactory::createCache($configLoader);
40 Factory\DBFactory::init($configCache, $_SERVER);
41 $config = Factory\ConfigFactory::createConfig($configCache);
42 // needed to call PConfig::init()
43 Factory\ConfigFactory::createPConfig($configCache);
44 $logger = Factory\LoggerFactory::create('daemon', $config);
45 $profiler = Factory\ProfilerFactory::create($logger, $config);
47 $a = new App($config, $logger, $profiler);
49 if ($a->getMode()->isInstall()) {
50 die("Friendica isn't properly installed yet.\n");
55 if (empty(Config::get('system', 'pidfile'))) {
57 Please set system.pidfile in config/local.config.php. For example:
60 'pidfile' => '/path/to/daemon.pid',
66 $pidfile = Config::get('system', 'pidfile');
68 if (in_array("start", $_SERVER["argv"])) {
72 if (in_array("stop", $_SERVER["argv"])) {
76 if (in_array("status", $_SERVER["argv"])) {
80 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
83 die("Please use either 'start', 'stop' or 'status'.\n");
86 if (empty($_SERVER["argv"][0])) {
87 die("Unexpected script behaviour. This message should never occur.\n");
92 if (is_readable($pidfile)) {
93 $pid = intval(file_get_contents($pidfile));
96 if (empty($pid) && in_array($mode, ["stop", "status"])) {
97 Config::set('system', 'worker_daemon_mode', false);
98 die("Pidfile wasn't found. Is the daemon running?\n");
101 if ($mode == "status") {
102 if (posix_kill($pid, 0)) {
103 die("Daemon process $pid is running.\n");
108 Config::set('system', 'worker_daemon_mode', false);
109 die("Daemon process $pid isn't running.\n");
112 if ($mode == "stop") {
113 posix_kill($pid, SIGTERM);
117 $logger->notice("Worker daemon process was killed", ["pid" => $pid]);
119 Config::set('system', 'worker_daemon_mode', false);
120 die("Worker daemon process $pid was killed.\n");
123 if (!empty($pid) && posix_kill($pid, 0)) {
124 die("Daemon process $pid is already running.\n");
127 $logger->notice('Starting worker daemon.', ["pid" => $pid]);
130 echo "Starting worker daemon.\n";
132 // Switch over to daemon mode.
133 if ($pid = pcntl_fork()) {
137 fclose(STDIN); // Close all of the standard
139 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
140 // fclose(STDOUT); // file descriptors as we
141 // fclose(STDERR); // are running as a daemon.
145 register_shutdown_function('shutdown');
147 if (posix_setsid() < 0) {
151 if ($pid = pcntl_fork()) {
156 file_put_contents($pidfile, $pid);
158 // We lose the database connection upon forking
159 Factory\DBFactory::init($configCache, $_SERVER);
162 Config::set('system', 'worker_daemon_mode', true);
164 // Just to be sure that this script really runs endlessly
167 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
172 // Now running as a daemon.
174 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
175 $logger->info('Forcing cron worker call.', ["pid" => $pid]);
179 Worker::spawnWorker($do_cron);
182 // We force a reconnect of the database connection.
183 // This is done to ensure that the connection don't get lost over time.
189 $logger->info("Sleeping", ["pid" => $pid]);
192 $seconds = (time() - $start);
194 // logarithmic wait time calculation.
195 // Background: After jobs had been started, they often fork many workers.
196 // To not waste too much time, the sleep period increases.
197 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
198 $sleep = round(log10($arg) * 1000000, 0);
201 $timeout = ($seconds >= $wait_interval);
202 } while (!$timeout && !Worker::IPCJobsExists());
206 $logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
209 $logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
213 function shutdown() {
214 posix_kill(posix_getpid(), SIGHUP);