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