]> git.mxchange.org Git - friendica.git/blob - bin/daemon.php
Add numeric field_input to frio
[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 use Psr\Log\LoggerInterface;
16
17 // Get options
18 $shortopts = 'f';
19 $longopts = ['foreground'];
20 $options = getopt($shortopts, $longopts);
21
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]);
25
26         if (substr($directory, 0, 1) != "/") {
27                 $directory = $_SERVER["PWD"] . "/" . $directory;
28         }
29         $directory = realpath($directory . "/..");
30
31         chdir($directory);
32 }
33
34 require dirname(__DIR__) . '/vendor/autoload.php';
35
36 $dice = (new Dice())->addRules(include __DIR__ . '/../static/dependencies.config.php');
37 $dice = $dice->addRule(LoggerInterface::class,['constructParams' => ['daemon']]);
38
39 \Friendica\BaseObject::setDependencyInjection($dice);
40 $a = \Friendica\BaseObject::getApp();
41
42 if ($a->getMode()->isInstall()) {
43         die("Friendica isn't properly installed yet.\n");
44 }
45
46 Config::load();
47
48 if (empty(Config::get('system', 'pidfile'))) {
49         die(<<<TXT
50 Please set system.pidfile in config/local.config.php. For example:
51     
52     'system' => [ 
53         'pidfile' => '/path/to/daemon.pid',
54     ],
55 TXT
56     );
57 }
58
59 $pidfile = Config::get('system', 'pidfile');
60
61 if (in_array("start", $_SERVER["argv"])) {
62         $mode = "start";
63 }
64
65 if (in_array("stop", $_SERVER["argv"])) {
66         $mode = "stop";
67 }
68
69 if (in_array("status", $_SERVER["argv"])) {
70         $mode = "status";
71 }
72
73 $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
74
75 if (!isset($mode)) {
76         die("Please use either 'start', 'stop' or 'status'.\n");
77 }
78
79 if (empty($_SERVER["argv"][0])) {
80         die("Unexpected script behaviour. This message should never occur.\n");
81 }
82
83 $pid = null;
84
85 if (is_readable($pidfile)) {
86         $pid = intval(file_get_contents($pidfile));
87 }
88
89 if (empty($pid) && in_array($mode, ["stop", "status"])) {
90         Config::set('system', 'worker_daemon_mode', false);
91         die("Pidfile wasn't found. Is the daemon running?\n");
92 }
93
94 if ($mode == "status") {
95         if (posix_kill($pid, 0)) {
96                 die("Daemon process $pid is running.\n");
97         }
98
99         unlink($pidfile);
100
101         Config::set('system', 'worker_daemon_mode', false);
102         die("Daemon process $pid isn't running.\n");
103 }
104
105 if ($mode == "stop") {
106         posix_kill($pid, SIGTERM);
107
108         unlink($pidfile);
109
110         Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
111
112         Config::set('system', 'worker_daemon_mode', false);
113         die("Worker daemon process $pid was killed.\n");
114 }
115
116 if (!empty($pid) && posix_kill($pid, 0)) {
117         die("Daemon process $pid is already running.\n");
118 }
119
120 Logger::notice('Starting worker daemon.', ["pid" => $pid]);
121
122 if (!$foreground) {
123         echo "Starting worker daemon.\n";
124
125         // Switch over to daemon mode.
126         if ($pid = pcntl_fork()) {
127                 return;     // Parent
128         }
129
130         fclose(STDIN);  // Close all of the standard
131
132         // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
133         // fclose(STDOUT); // file descriptors as we
134         // fclose(STDERR); // are running as a daemon.
135
136         DBA::disconnect();
137
138         register_shutdown_function('shutdown');
139
140         if (posix_setsid() < 0) {
141                 return;
142         }
143
144         if ($pid = pcntl_fork()) {
145                 return;     // Parent
146         }
147
148         $pid = getmypid();
149         file_put_contents($pidfile, $pid);
150
151         // We lose the database connection upon forking
152         DBA::reconnect();
153 }
154
155 Config::set('system', 'worker_daemon_mode', true);
156
157 // Just to be sure that this script really runs endlessly
158 set_time_limit(0);
159
160 $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
161
162 $do_cron = true;
163 $last_cron = 0;
164
165 // Now running as a daemon.
166 while (true) {
167         if (!$do_cron && ($last_cron + $wait_interval) < time()) {
168                 Logger::info('Forcing cron worker call.', ["pid" => $pid]);
169                 $do_cron = true;
170         }
171
172         Worker::spawnWorker($do_cron);
173
174         if ($do_cron) {
175                 // We force a reconnect of the database connection.
176                 // This is done to ensure that the connection don't get lost over time.
177                 DBA::reconnect();
178
179                 $last_cron = time();
180         }
181
182         Logger::info("Sleeping", ["pid" => $pid]);
183         $start = time();
184         do {
185                 $seconds = (time() - $start);
186
187                 // logarithmic wait time calculation.
188                 // Background: After jobs had been started, they often fork many workers.
189                 // To not waste too much time, the sleep period increases.
190                 $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
191                 $sleep = round(log10($arg) * 1000000, 0);
192                 usleep($sleep);
193
194                 $timeout = ($seconds >= $wait_interval);
195         } while (!$timeout && !Worker::IPCJobsExists());
196
197         if ($timeout) {
198                 $do_cron = true;
199                 Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
200         } else {
201                 $do_cron = false;
202                 Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
203         }
204 }
205
206 function shutdown() {
207         posix_kill(posix_getpid(), SIGHUP);
208 }