]> git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
remove $link
[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
13 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
14 define('DAEMON_EXTENDED_DEBUG', true);
15 }
16
17 require_once "autoload.php";
18 require_once "functions.php";
19 require_once "rssfuncs.php";
20 require_once "sanity_check.php";
21 require_once "config.php";
22 require_once "db.php";
23 require_once "db-prefs.php";
24 require_once "errorhandler.php";
25
26 // defaults
27 define('PURGE_INTERVAL', 3600); // seconds
28 define('MAX_CHILD_RUNTIME', 600); // seconds
29 define('MAX_JOBS', 2);
30 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
31
32 if (!function_exists('pcntl_fork')) {
33 die("error: This script requires PHP compiled with PCNTL module.\n");
34 }
35
36 $master_handlers_installed = false;
37
38 $children = array();
39 $ctimes = array();
40
41 $last_checkpoint = -1;
42
43 function reap_children() {
44 global $children;
45 global $ctimes;
46
47 $tmp = array();
48
49 foreach ($children as $pid) {
50 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
51
52 if (file_is_locked("update_daemon-$pid.lock")) {
53 array_push($tmp, $pid);
54 } else {
55 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
56 unset($ctimes[$pid]);
57
58 }
59 } else {
60 _debug("[reap_children] child $pid reaped.");
61 unset($ctimes[$pid]);
62 }
63 }
64
65 $children = $tmp;
66
67 return count($tmp);
68 }
69
70 function check_ctimes() {
71 global $ctimes;
72
73 foreach (array_keys($ctimes) as $pid) {
74 $started = $ctimes[$pid];
75
76 if (time() - $started > MAX_CHILD_RUNTIME) {
77 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
78 posix_kill($pid, SIGKILL);
79 }
80 }
81 }
82
83 function sigchld_handler($signal) {
84 $running_jobs = reap_children();
85
86 _debug("[SIGCHLD] jobs left: $running_jobs");
87
88 pcntl_waitpid(-1, $status, WNOHANG);
89 }
90
91 function shutdown($caller_pid) {
92 if ($caller_pid == posix_getpid()) {
93 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
94 _debug("removing lockfile (master)...");
95 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
96 }
97 }
98 }
99
100 function task_shutdown() {
101 $pid = posix_getpid();
102
103 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
104 _debug("removing lockfile ($pid)...");
105 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
106 }
107 }
108
109 function sigint_handler() {
110 _debug("[MASTER] SIG_INT received.\n");
111 shutdown(posix_getpid());
112 die;
113 }
114
115 function task_sigint_handler() {
116 _debug("[TASK] SIG_INT received.\n");
117 task_shutdown();
118 die;
119 }
120
121 pcntl_signal(SIGCHLD, 'sigchld_handler');
122
123 $longopts = array("log:",
124 "tasks:",
125 "interval:",
126 "quiet",
127 "help");
128
129 $options = getopt("", $longopts);
130
131 if (isset($options["help"]) ) {
132 print "Tiny Tiny RSS update daemon.\n\n";
133 print "Options:\n";
134 print " --log FILE - log messages to FILE\n";
135 print " --tasks N - amount of update tasks to spawn\n";
136 print " default: " . MAX_JOBS . "\n";
137 print " --interval N - task spawn interval\n";
138 print " default: " . SPAWN_INTERVAL . " seconds.\n";
139 print " --quiet - don't output messages to stdout\n";
140 return;
141 }
142
143 define('QUIET', isset($options['quiet']));
144
145 if (isset($options["tasks"])) {
146 _debug("Set to spawn " . $options["tasks"] . " children.");
147 $max_jobs = $options["tasks"];
148 } else {
149 $max_jobs = MAX_JOBS;
150 }
151
152 if (isset($options["interval"])) {
153 _debug("Spawn interval: " . $options["interval"] . " seconds.");
154 $spawn_interval = $options["interval"];
155 } else {
156 $spawn_interval = SPAWN_INTERVAL;
157 }
158
159 if (isset($options["log"])) {
160 _debug("Logging to " . $options["log"]);
161 define('LOGFILE', $options["log"]);
162 }
163
164 if (file_is_locked("update_daemon.lock")) {
165 die("error: Can't create lockfile. ".
166 "Maybe another daemon is already running.\n");
167 }
168
169 // Try to lock a file in order to avoid concurrent update.
170 $lock_handle = make_lockfile("update_daemon.lock");
171
172 if (!$lock_handle) {
173 die("error: Can't create lockfile. ".
174 "Maybe another daemon is already running.\n");
175 }
176
177 init_plugins();
178
179 $schema_version = get_schema_version();
180
181 db_close();
182
183 if ($schema_version != SCHEMA_VERSION) {
184 die("Schema version is wrong, please upgrade the database.\n");
185 }
186
187 while (true) {
188
189 // Since sleep is interupted by SIGCHLD, we need another way to
190 // respect the spawn interval
191 $next_spawn = $last_checkpoint + $spawn_interval - time();
192
193 if ($next_spawn % 60 == 0) {
194 $running_jobs = count($children);
195 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
196 }
197
198 if ($last_checkpoint + $spawn_interval < time()) {
199
200 /* Check if schema version changed */
201
202 init_plugins();
203 $test_schema_version = get_schema_version();
204
205 if ($test_schema_version != $schema_version) {
206 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
207 echo "Schema version changed while we were running, bailing out\n";
208 exit(100);
209 }
210
211 check_ctimes();
212 reap_children();
213
214 for ($j = count($children); $j < $max_jobs; $j++) {
215 $pid = pcntl_fork();
216 if ($pid == -1) {
217 die("fork failed!\n");
218 } else if ($pid) {
219
220 if (!$master_handlers_installed) {
221 _debug("[MASTER] installing shutdown handlers");
222 pcntl_signal(SIGINT, 'sigint_handler');
223 register_shutdown_function('shutdown', posix_getpid());
224 $master_handlers_installed = true;
225 }
226
227 _debug("[MASTER] spawned client $j [PID:$pid]...");
228 array_push($children, $pid);
229 $ctimes[$pid] = time();
230 } else {
231 pcntl_signal(SIGCHLD, SIG_IGN);
232 pcntl_signal(SIGINT, 'task_sigint_handler');
233
234 register_shutdown_function('task_shutdown');
235
236 $my_pid = posix_getpid();
237 $lock_filename = "update_daemon-$my_pid.lock";
238
239 $lock_handle = make_lockfile($lock_filename);
240
241 if (!$lock_handle) {
242 die("error: Can't create lockfile ($lock_filename). ".
243 "Maybe another daemon is already running.\n");
244 }
245
246 // ****** Updating RSS code *******
247 // Only run in fork process.
248
249 $start_timestamp = time();
250
251 if (!init_plugins()) return;
252
253 // We disable stamp file, since it is of no use in a multiprocess update.
254 // not really, tho for the time being -fox
255 if (!make_stampfile('update_daemon.stamp')) {
256 _debug("warning: unable to create stampfile\n");
257 }
258
259 // Call to the feed batch update function
260 // and maybe regenerate feedbrowser cache
261
262 $nf = 0;
263
264 _debug("Waiting before update [$j]..");
265 sleep($j*5);
266 $nf = update_daemon_common();
267
268 if (rand(0,100) > 50) {
269 $count = update_feedbrowser_cache();
270 _debug("Feedbrowser updated, $count feeds processed.");
271
272 purge_orphans( true);
273
274 $rc = cleanup_tags( 14, 50000);
275
276 _debug("Cleaned $rc cached tags.");
277
278 global $pluginhost;
279 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
280 }
281
282 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
283
284 if ($nf > 0) {
285 _debug("Feeds processed: $nf");
286
287 if (time() - $start_timestamp > 0) {
288 _debug("Feeds/minute: " . sprintf("%.2d", $nf/((time()-$start_timestamp)/60)));
289 }
290 }
291
292 db_close();
293
294 // We are in a fork.
295 // We wait a little before exiting to avoid to be faster than our parent process.
296 sleep(1);
297
298 unlink(LOCK_DIRECTORY . "/$lock_filename");
299
300 // We exit in order to avoid fork bombing.
301 exit(0);
302 }
303 }
304 $last_checkpoint = time();
305 }
306 sleep(1);
307 }
308
309 ?>