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