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