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