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 $pconfig = Factory\ConfigFactory::createPConfig($configCache);
43 $logger = Factory\LoggerFactory::create('daemon', $config);
44 $profiler = Factory\ProfilerFactory::create($logger, $config);
46 $a = new App($config, $logger, $profiler);
48 if ($a->getMode()->isInstall()) {
49 die("Friendica isn't properly installed yet.\n");
54 if (empty(Config::get('system', 'pidfile'))) {
56 Please set system.pidfile in config/local.config.php. For example:
59 'pidfile' => '/path/to/daemon.pid',
65 $pidfile = Config::get('system', 'pidfile');
67 if (in_array("start", $_SERVER["argv"])) {
71 if (in_array("stop", $_SERVER["argv"])) {
75 if (in_array("status", $_SERVER["argv"])) {
79 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
82 die("Please use either 'start', 'stop' or 'status'.\n");
85 if (empty($_SERVER["argv"][0])) {
86 die("Unexpected script behaviour. This message should never occur.\n");
91 if (is_readable($pidfile)) {
92 $pid = intval(file_get_contents($pidfile));
95 if (empty($pid) && in_array($mode, ["stop", "status"])) {
96 Config::set('system', 'worker_daemon_mode', false);
97 die("Pidfile wasn't found. Is the daemon running?\n");
100 if ($mode == "status") {
101 if (posix_kill($pid, 0)) {
102 die("Daemon process $pid is running.\n");
107 Config::set('system', 'worker_daemon_mode', false);
108 die("Daemon process $pid isn't running.\n");
111 if ($mode == "stop") {
112 posix_kill($pid, SIGTERM);
116 $logger->notice("Worker daemon process was killed", ["pid" => $pid]);
118 Config::set('system', 'worker_daemon_mode', false);
119 die("Worker daemon process $pid was killed.\n");
122 if (!empty($pid) && posix_kill($pid, 0)) {
123 die("Daemon process $pid is already running.\n");
126 $logger->notice('Starting worker daemon.', ["pid" => $pid]);
129 echo "Starting worker daemon.\n";
131 // Switch over to daemon mode.
132 if ($pid = pcntl_fork()) {
136 fclose(STDIN); // Close all of the standard
138 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
139 // fclose(STDOUT); // file descriptors as we
140 // fclose(STDERR); // are running as a daemon.
144 register_shutdown_function('shutdown');
146 if (posix_setsid() < 0) {
150 if ($pid = pcntl_fork()) {
155 file_put_contents($pidfile, $pid);
157 // We lose the database connection upon forking
158 Factory\DBFactory::init($configCache, $_SERVER);
161 Config::set('system', 'worker_daemon_mode', true);
163 // Just to be sure that this script really runs endlessly
166 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
171 // Now running as a daemon.
173 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
174 $logger->info('Forcing cron worker call.', ["pid" => $pid]);
178 Worker::spawnWorker($do_cron);
181 // We force a reconnect of the database connection.
182 // This is done to ensure that the connection don't get lost over time.
188 $logger->info("Sleeping", ["pid" => $pid]);
191 $seconds = (time() - $start);
193 // logarithmic wait time calculation.
194 // Background: After jobs had been started, they often fork many workers.
195 // To not waste too much time, the sleep period increases.
196 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
197 $sleep = round(log10($arg) * 1000000, 0);
200 $timeout = ($seconds >= $wait_interval);
201 } while (!$timeout && !Worker::IPCJobsExists());
205 $logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
208 $logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
212 function shutdown() {
213 posix_kill(posix_getpid(), SIGHUP);