]> git.mxchange.org Git - friendica.git/blob - bin/daemon.php
Display pending contacts in the contact list
[friendica.git] / bin / daemon.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * @file bin/daemon.php
5  * @brief Run the worker from a daemon.
6  *
7  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
8  */
9
10 use Dice\Dice;
11 use Friendica\Core\Config;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Worker;
14 use Friendica\Database\DBA;
15
16 // Get options
17 $shortopts = 'f';
18 $longopts = ['foreground'];
19 $options = getopt($shortopts, $longopts);
20
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]);
24
25         if (substr($directory, 0, 1) != "/") {
26                 $directory = $_SERVER["PWD"] . "/" . $directory;
27         }
28         $directory = realpath($directory . "/..");
29
30         chdir($directory);
31 }
32
33 require dirname(__DIR__) . '/vendor/autoload.php';
34
35 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
36
37 \Friendica\BaseObject::setDependencyInjection($dice);
38 $a = \Friendica\BaseObject::getApp();
39
40 if ($a->getMode()->isInstall()) {
41         die("Friendica isn't properly installed yet.\n");
42 }
43
44 Config::load();
45
46 if (empty(Config::get('system', 'pidfile'))) {
47         die(<<<TXT
48 Please set system.pidfile in config/local.config.php. For example:
49     
50     'system' => [ 
51         'pidfile' => '/path/to/daemon.pid',
52     ],
53 TXT
54     );
55 }
56
57 $pidfile = Config::get('system', 'pidfile');
58
59 if (in_array("start", $_SERVER["argv"])) {
60         $mode = "start";
61 }
62
63 if (in_array("stop", $_SERVER["argv"])) {
64         $mode = "stop";
65 }
66
67 if (in_array("status", $_SERVER["argv"])) {
68         $mode = "status";
69 }
70
71 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
72
73 if (!isset($mode)) {
74         die("Please use either 'start', 'stop' or 'status'.\n");
75 }
76
77 if (empty($_SERVER["argv"][0])) {
78         die("Unexpected script behaviour. This message should never occur.\n");
79 }
80
81 $pid = null;
82
83 if (is_readable($pidfile)) {
84         $pid = intval(file_get_contents($pidfile));
85 }
86
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");
90 }
91
92 if ($mode == "status") {
93         if (posix_kill($pid, 0)) {
94                 die("Daemon process $pid is running.\n");
95         }
96
97         unlink($pidfile);
98
99         Config::set('system', 'worker_daemon_mode', false);
100         die("Daemon process $pid isn't running.\n");
101 }
102
103 if ($mode == "stop") {
104         posix_kill($pid, SIGTERM);
105
106         unlink($pidfile);
107
108         Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
109
110         Config::set('system', 'worker_daemon_mode', false);
111         die("Worker daemon process $pid was killed.\n");
112 }
113
114 if (!empty($pid) && posix_kill($pid, 0)) {
115         die("Daemon process $pid is already running.\n");
116 }
117
118 Logger::notice('Starting worker daemon.', ["pid" => $pid]);
119
120 if (!$foreground) {
121         echo "Starting worker daemon.\n";
122
123         // Switch over to daemon mode.
124         if ($pid = pcntl_fork()) {
125                 return;     // Parent
126         }
127
128         fclose(STDIN);  // Close all of the standard
129
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.
133
134         DBA::disconnect();
135
136         register_shutdown_function('shutdown');
137
138         if (posix_setsid() < 0) {
139                 return;
140         }
141
142         if ($pid = pcntl_fork()) {
143                 return;     // Parent
144         }
145
146         $pid = getmypid();
147         file_put_contents($pidfile, $pid);
148
149         // We lose the database connection upon forking
150         DBA::reconnect();
151 }
152
153 Config::set('system', 'worker_daemon_mode', true);
154
155 // Just to be sure that this script really runs endlessly
156 set_time_limit(0);
157
158 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
159
160 $do_cron = true;
161 $last_cron = 0;
162
163 // Now running as a daemon.
164 while (true) {
165         if (!$do_cron && ($last_cron + $wait_interval) < time()) {
166                 Logger::info('Forcing cron worker call.', ["pid" => $pid]);
167                 $do_cron = true;
168         }
169
170         Worker::spawnWorker($do_cron);
171
172         if ($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.
175                 DBA::reconnect();
176
177                 $last_cron = time();
178         }
179
180         Logger::info("Sleeping", ["pid" => $pid]);
181         $start = time();
182         do {
183                 $seconds = (time() - $start);
184
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);
190                 usleep($sleep);
191
192                 $timeout = ($seconds >= $wait_interval);
193         } while (!$timeout && !Worker::IPCJobsExists());
194
195         if ($timeout) {
196                 $do_cron = true;
197                 Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
198         } else {
199                 $do_cron = false;
200                 Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
201         }
202 }
203
204 function shutdown() {
205         posix_kill(posix_getpid(), SIGHUP);
206 }