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\Factory;
15 use Friendica\Util\BasePath;
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 $basedir = BasePath::create(dirname(__DIR__), $_SERVER);
37 $configLoader = new Config\ConfigCacheLoader($basedir);
38 $config = Factory\ConfigFactory::createCache($configLoader);
39 $logger = Factory\LoggerFactory::create('daemon', $config);
40 $profiler = Factory\ProfilerFactory::create($logger, $config);
42 $a = new App($config, $logger, $profiler);
44 if ($a->getMode()->isInstall()) {
45 die("Friendica isn't properly installed yet.\n");
50 if (empty(Config::get('system', 'pidfile'))) {
52 Please set system.pidfile in config/local.config.php. For example:
55 'pidfile' => '/path/to/daemon.pid',
61 $pidfile = Config::get('system', 'pidfile');
63 if (in_array("start", $_SERVER["argv"])) {
67 if (in_array("stop", $_SERVER["argv"])) {
71 if (in_array("status", $_SERVER["argv"])) {
75 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
78 die("Please use either 'start', 'stop' or 'status'.\n");
81 if (empty($_SERVER["argv"][0])) {
82 die("Unexpected script behaviour. This message should never occur.\n");
87 if (is_readable($pidfile)) {
88 $pid = intval(file_get_contents($pidfile));
91 if (empty($pid) && in_array($mode, ["stop", "status"])) {
92 Config::set('system', 'worker_daemon_mode', false);
93 die("Pidfile wasn't found. Is the daemon running?\n");
96 if ($mode == "status") {
97 if (posix_kill($pid, 0)) {
98 die("Daemon process $pid is running.\n");
103 Config::set('system', 'worker_daemon_mode', false);
104 die("Daemon process $pid isn't running.\n");
107 if ($mode == "stop") {
108 posix_kill($pid, SIGTERM);
112 $logger->notice("Worker daemon process was killed", ["pid" => $pid]);
114 Config::set('system', 'worker_daemon_mode', false);
115 die("Worker daemon process $pid was killed.\n");
118 if (!empty($pid) && posix_kill($pid, 0)) {
119 die("Daemon process $pid is already running.\n");
122 $logger->notice('Starting worker daemon.', ["pid" => $pid]);
125 echo "Starting worker daemon.\n";
127 // Switch over to daemon mode.
128 if ($pid = pcntl_fork()) {
132 fclose(STDIN); // Close all of the standard
134 // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
135 // fclose(STDOUT); // file descriptors as we
136 // fclose(STDERR); // are running as a daemon.
140 register_shutdown_function('shutdown');
142 if (posix_setsid() < 0) {
146 if ($pid = pcntl_fork()) {
151 file_put_contents($pidfile, $pid);
153 // We lose the database connection upon forking
157 Config::set('system', 'worker_daemon_mode', true);
159 // Just to be sure that this script really runs endlessly
162 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
167 // Now running as a daemon.
169 if (!$do_cron && ($last_cron + $wait_interval) < time()) {
170 $logger->info('Forcing cron worker call.', ["pid" => $pid]);
174 Worker::spawnWorker($do_cron);
177 // We force a reconnect of the database connection.
178 // This is done to ensure that the connection don't get lost over time.
184 $logger->info("Sleeping", ["pid" => $pid]);
187 $seconds = (time() - $start);
189 // logarithmic wait time calculation.
190 // Background: After jobs had been started, they often fork many workers.
191 // To not waste too much time, the sleep period increases.
192 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
193 $sleep = round(log10($arg) * 1000000, 0);
196 $timeout = ($seconds >= $wait_interval);
197 } while (!$timeout && !Worker::IPCJobsExists());
201 $logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
204 $logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
208 function shutdown() {
209 posix_kill(posix_getpid(), SIGHUP);