]> git.wh0rd.org Git - tt-rss.git/blob - update_daemon2.php
subscribe_to_feed: force-cast login and password to string
[tt-rss.git] / update_daemon2.php
1 #!/usr/bin/env php
2 <?php
3         set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
4                 get_include_path());
5
6         declare(ticks = 1);
7         chdir(dirname(__FILE__));
8
9         define('DISABLE_SESSIONS', true);
10
11         require_once "version.php";
12         require_once "autoload.php";
13         require_once "functions.php";
14         require_once "config.php";
15
16         // defaults
17         define_default('PURGE_INTERVAL', 3600); // seconds
18         define_default('MAX_CHILD_RUNTIME', 1800); // seconds
19         define_default('MAX_JOBS', 2);
20         define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
21
22         require_once "sanity_check.php";
23         require_once "db.php";
24         require_once "db-prefs.php";
25
26         if (!function_exists('pcntl_fork')) {
27                 die("error: This script requires PHP compiled with PCNTL module.\n");
28         }
29
30         $options = getopt("");
31
32         if (!is_array($options)) {
33                 die("error: getopt() failed. ".
34                         "Most probably you are using PHP CGI to run this script ".
35                         "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
36                         "additional information.\n");
37         }
38
39
40         $master_handlers_installed = false;
41
42         $children = array();
43         $ctimes = array();
44
45         $last_checkpoint = -1;
46
47         /**
48          * @SuppressWarnings(unused)
49          */
50         function reap_children() {
51                 global $children;
52                 global $ctimes;
53
54                 $tmp = array();
55
56                 foreach ($children as $pid) {
57                         if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
58
59                                 if (file_is_locked("update_daemon-$pid.lock")) {
60                                         array_push($tmp, $pid);
61                                 } else {
62                                         _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
63                                         unset($ctimes[$pid]);
64
65                                 }
66                         } else {
67                                 _debug("[reap_children] child $pid reaped.");
68                                 unset($ctimes[$pid]);
69                         }
70                 }
71
72                 $children = $tmp;
73
74                 return count($tmp);
75         }
76
77         function check_ctimes() {
78                 global $ctimes;
79
80                 foreach (array_keys($ctimes) as $pid) {
81                         $started = $ctimes[$pid];
82
83                         if (time() - $started > MAX_CHILD_RUNTIME) {
84                                 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
85                                 posix_kill($pid, SIGKILL);
86                         }
87                 }
88         }
89
90         /**
91         * @SuppressWarnings(unused)
92         */
93         function sigchld_handler($signal) {
94                 $running_jobs = reap_children();
95
96                 _debug("[SIGCHLD] jobs left: $running_jobs");
97
98                 pcntl_waitpid(-1, $status, WNOHANG);
99         }
100
101         function shutdown($caller_pid) {
102                 if ($caller_pid == posix_getpid()) {
103                         if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
104                                 _debug("removing lockfile (master)...");
105                                 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
106                         }
107                 }
108         }
109
110         function task_shutdown() {
111                 $pid = posix_getpid();
112
113                 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
114                         _debug("removing lockfile ($pid)...");
115                         unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
116                 }
117         }
118
119         function sigint_handler() {
120                 _debug("[MASTER] SIG_INT received.\n");
121                 shutdown(posix_getpid());
122                 die;
123         }
124
125         function task_sigint_handler() {
126                 _debug("[TASK] SIG_INT received.\n");
127                 task_shutdown();
128                 die;
129         }
130
131         pcntl_signal(SIGCHLD, 'sigchld_handler');
132
133         $longopts = array("log:",
134                         "tasks:",
135                         "interval:",
136                         "quiet",
137                         "help");
138
139         $options = getopt("", $longopts);
140
141         if (isset($options["help"]) ) {
142                 print "Tiny Tiny RSS update daemon.\n\n";
143                 print "Options:\n";
144                 print "  --log FILE           - log messages to FILE\n";
145                 print "  --tasks N            - amount of update tasks to spawn\n";
146                 print "                         default: " . MAX_JOBS . "\n";
147                 print "  --interval N         - task spawn interval\n";
148                 print "                         default: " . SPAWN_INTERVAL . " seconds.\n";
149                 print "  --quiet              - don't output messages to stdout\n";
150                 return;
151         }
152
153         define('QUIET', isset($options['quiet']));
154
155         if (isset($options["tasks"])) {
156                 _debug("Set to spawn " . $options["tasks"] . " children.");
157                 $max_jobs = $options["tasks"];
158         } else {
159                 $max_jobs = MAX_JOBS;
160         }
161
162         if (isset($options["interval"])) {
163                 _debug("Spawn interval: " . $options["interval"] . " seconds.");
164                 $spawn_interval = $options["interval"];
165         } else {
166                 $spawn_interval = SPAWN_INTERVAL;
167         }
168
169         // let's enforce a minimum spawn interval as to not forkbomb the host
170         $spawn_interval = max(60, $spawn_interval);
171         _debug("Spawn interval: $spawn_interval sec");
172
173         if (isset($options["log"])) {
174                 _debug("Logging to " . $options["log"]);
175                 define('LOGFILE', $options["log"]);
176         }
177
178         if (file_is_locked("update_daemon.lock")) {
179                 die("error: Can't create lockfile. ".
180                         "Maybe another daemon is already running.\n");
181         }
182
183         // Try to lock a file in order to avoid concurrent update.
184         $lock_handle = make_lockfile("update_daemon.lock");
185
186         if (!$lock_handle) {
187                 die("error: Can't create lockfile. ".
188                         "Maybe another daemon is already running.\n");
189         }
190
191         $schema_version = get_schema_version();
192
193         if ($schema_version != SCHEMA_VERSION) {
194                 die("Schema version is wrong, please upgrade the database.\n");
195         }
196
197         // Protip: children close shared database handle when terminating, it's a bad idea to
198         // do database stuff on main process from now on.
199
200         while (true) {
201
202                 // Since sleep is interupted by SIGCHLD, we need another way to
203                 // respect the spawn interval
204                 $next_spawn = $last_checkpoint + $spawn_interval - time();
205
206                 if ($next_spawn % 60 == 0) {
207                         $running_jobs = count($children);
208                         _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
209                 }
210
211                 if ($last_checkpoint + $spawn_interval < time()) {
212                         check_ctimes();
213                         reap_children();
214
215                         for ($j = count($children); $j < $max_jobs; $j++) {
216                                 $pid = pcntl_fork();
217                                 if ($pid == -1) {
218                                         die("fork failed!\n");
219                                 } else if ($pid) {
220
221                                         if (!$master_handlers_installed) {
222                                                 _debug("[MASTER] installing shutdown handlers");
223                                                 pcntl_signal(SIGINT, 'sigint_handler');
224                                                 pcntl_signal(SIGTERM, 'sigint_handler');
225                                                 register_shutdown_function('shutdown', posix_getpid());
226                                                 $master_handlers_installed = true;
227                                         }
228
229                                         _debug("[MASTER] spawned client $j [PID:$pid]...");
230                                         array_push($children, $pid);
231                                         $ctimes[$pid] = time();
232                                 } else {
233                                         pcntl_signal(SIGCHLD, SIG_IGN);
234                                         pcntl_signal(SIGINT, 'task_sigint_handler');
235
236                                         register_shutdown_function('task_shutdown');
237
238                                         $quiet = (isset($options["quiet"])) ? "--quiet" : "";
239                                         $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
240
241                                         $my_pid = posix_getpid();
242
243                                         passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
244
245                                         sleep(1);
246
247                                         // We exit in order to avoid fork bombing.
248                                         exit(0);
249                                 }
250                         }
251                         $last_checkpoint = time();
252                 }
253                 sleep(1);
254         }
255
256 ?>