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