]>
Commit | Line | Data |
---|---|---|
bf0bedcb | 1 | #!/usr/bin/env php |
02008cb1 | 2 | <?php |
88e8fb3a AD |
3 | set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR . |
4 | get_include_path()); | |
ffa7cbae | 5 | |
02008cb1 | 6 | declare(ticks = 1); |
9b27cec8 | 7 | chdir(dirname(__FILE__)); |
02008cb1 | 8 | |
ffa7cbae | 9 | define('DISABLE_SESSIONS', true); |
02008cb1 | 10 | |
ffa7cbae | 11 | require_once "version.php"; |
0e6bdaef | 12 | require_once "config.php"; |
404e2e36 | 13 | require_once "autoload.php"; |
9765e8b9 | 14 | require_once "functions.php"; |
0e6bdaef AD |
15 | |
16 | // defaults | |
17 | define_default('PURGE_INTERVAL', 3600); // seconds | |
18 | define_default('MAX_CHILD_RUNTIME', 600); // seconds | |
19 | define_default('MAX_JOBS', 2); | |
20 | define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds | |
21 | define_default('DAEMON_FEED_LIMIT', 250); | |
22 | ||
9765e8b9 | 23 | require_once "rssfuncs.php"; |
ffa7cbae | 24 | require_once "sanity_check.php"; |
5893edd5 AD |
25 | require_once "db.php"; |
26 | require_once "db-prefs.php"; | |
ffa7cbae | 27 | |
ffa7cbae | 28 | |
16956646 AD |
29 | if (!function_exists('pcntl_fork')) { |
30 | die("error: This script requires PHP compiled with PCNTL module.\n"); | |
31 | } | |
32 | ||
724b7942 AD |
33 | $master_handlers_installed = false; |
34 | ||
e9338405 | 35 | $children = array(); |
c90a028c | 36 | $ctimes = array(); |
e9338405 | 37 | |
02008cb1 AD |
38 | $last_checkpoint = -1; |
39 | ||
5a613536 | 40 | function reap_children() { |
e9338405 | 41 | global $children; |
51ddf0f8 | 42 | global $ctimes; |
e9338405 AD |
43 | |
44 | $tmp = array(); | |
45 | ||
46 | foreach ($children as $pid) { | |
47 | if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { | |
7aabaa09 AD |
48 | |
49 | if (file_is_locked("update_daemon-$pid.lock")) { | |
8ccaff02 AD |
50 | array_push($tmp, $pid); |
51 | } else { | |
52 | _debug("[reap_children] child $pid seems active but lockfile is unlocked."); | |
125ab8db AD |
53 | unset($ctimes[$pid]); |
54 | ||
8ccaff02 | 55 | } |
e9338405 | 56 | } else { |
8ccaff02 | 57 | _debug("[reap_children] child $pid reaped."); |
c90a028c | 58 | unset($ctimes[$pid]); |
e9338405 AD |
59 | } |
60 | } | |
61 | ||
62 | $children = $tmp; | |
63 | ||
5a613536 AD |
64 | return count($tmp); |
65 | } | |
66 | ||
c90a028c AD |
67 | function check_ctimes() { |
68 | global $ctimes; | |
dbaa4e4a | 69 | |
c90a028c AD |
70 | foreach (array_keys($ctimes) as $pid) { |
71 | $started = $ctimes[$pid]; | |
72 | ||
73 | if (time() - $started > MAX_CHILD_RUNTIME) { | |
74 | _debug("[MASTER] child process $pid seems to be stuck, aborting..."); | |
0618b81c | 75 | posix_kill($pid, SIGKILL); |
c90a028c AD |
76 | } |
77 | } | |
5a613536 AD |
78 | } |
79 | ||
80 | function sigchld_handler($signal) { | |
81 | $running_jobs = reap_children(); | |
e9338405 AD |
82 | |
83 | _debug("[SIGCHLD] jobs left: $running_jobs"); | |
5a613536 | 84 | |
02008cb1 AD |
85 | pcntl_waitpid(-1, $status, WNOHANG); |
86 | } | |
87 | ||
cfe6d444 AD |
88 | function shutdown($caller_pid) { |
89 | if ($caller_pid == posix_getpid()) { | |
90 | if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) { | |
91 | _debug("removing lockfile (master)..."); | |
92 | unlink(LOCK_DIRECTORY . "/update_daemon.lock"); | |
93 | } | |
2cd099f0 | 94 | } |
8ccaff02 AD |
95 | } |
96 | ||
97 | function task_shutdown() { | |
98 | $pid = posix_getpid(); | |
99 | ||
2cd099f0 AD |
100 | if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) { |
101 | _debug("removing lockfile ($pid)..."); | |
8ccaff02 | 102 | unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock"); |
2cd099f0 | 103 | } |
8ccaff02 AD |
104 | } |
105 | ||
6a69e61f | 106 | function sigint_handler() { |
2cd099f0 | 107 | _debug("[MASTER] SIG_INT received.\n"); |
cfe6d444 | 108 | shutdown(posix_getpid()); |
2cd099f0 | 109 | die; |
dbaa4e4a | 110 | } |
8ccaff02 AD |
111 | |
112 | function task_sigint_handler() { | |
2cd099f0 | 113 | _debug("[TASK] SIG_INT received.\n"); |
8ccaff02 | 114 | task_shutdown(); |
2cd099f0 | 115 | die; |
dbaa4e4a | 116 | } |
6a69e61f | 117 | |
02008cb1 | 118 | pcntl_signal(SIGCHLD, 'sigchld_handler'); |
6a69e61f | 119 | |
dc24b520 AD |
120 | $longopts = array("log:", |
121 | "tasks:", | |
ec25336d | 122 | "interval:", |
db985423 | 123 | "quiet", |
dc24b520 AD |
124 | "help"); |
125 | ||
126 | $options = getopt("", $longopts); | |
127 | ||
128 | if (isset($options["help"]) ) { | |
129 | print "Tiny Tiny RSS update daemon.\n\n"; | |
130 | print "Options:\n"; | |
131 | print " --log FILE - log messages to FILE\n"; | |
132 | print " --tasks N - amount of update tasks to spawn\n"; | |
133 | print " default: " . MAX_JOBS . "\n"; | |
134 | print " --interval N - task spawn interval\n"; | |
135 | print " default: " . SPAWN_INTERVAL . " seconds.\n"; | |
136 | print " --quiet - don't output messages to stdout\n"; | |
137 | return; | |
138 | } | |
139 | ||
140 | define('QUIET', isset($options['quiet'])); | |
141 | ||
142 | if (isset($options["tasks"])) { | |
143 | _debug("Set to spawn " . $options["tasks"] . " children."); | |
ec25336d | 144 | $max_jobs = $options["tasks"]; |
dc24b520 AD |
145 | } else { |
146 | $max_jobs = MAX_JOBS; | |
147 | } | |
148 | ||
149 | if (isset($options["interval"])) { | |
150 | _debug("Spawn interval: " . $options["interval"] . " seconds."); | |
ec25336d | 151 | $spawn_interval = $options["interval"]; |
dc24b520 AD |
152 | } else { |
153 | $spawn_interval = SPAWN_INTERVAL; | |
154 | } | |
155 | ||
156 | if (isset($options["log"])) { | |
157 | _debug("Logging to " . $options["log"]); | |
158 | define('LOGFILE', $options["log"]); | |
159 | } | |
160 | ||
884c0a36 AD |
161 | if (file_is_locked("update_daemon.lock")) { |
162 | die("error: Can't create lockfile. ". | |
6a69e61f AD |
163 | "Maybe another daemon is already running.\n"); |
164 | } | |
02008cb1 | 165 | |
1a43a68c AD |
166 | // Try to lock a file in order to avoid concurrent update. |
167 | $lock_handle = make_lockfile("update_daemon.lock"); | |
168 | ||
169 | if (!$lock_handle) { | |
170 | die("error: Can't create lockfile. ". | |
171 | "Maybe another daemon is already running.\n"); | |
172 | } | |
173 | ||
6322ac79 | 174 | init_plugins(); |
ffa7cbae | 175 | |
6322ac79 | 176 | $schema_version = get_schema_version(); |
4c2da349 | 177 | |
857efe49 AD |
178 | if ($schema_version != SCHEMA_VERSION) { |
179 | die("Schema version is wrong, please upgrade the database.\n"); | |
180 | } | |
181 | ||
02008cb1 AD |
182 | while (true) { |
183 | ||
45004d43 | 184 | // Since sleep is interupted by SIGCHLD, we need another way to |
dc24b520 AD |
185 | // respect the spawn interval |
186 | $next_spawn = $last_checkpoint + $spawn_interval - time(); | |
02008cb1 | 187 | |
61aa7499 | 188 | if ($next_spawn % 60 == 0) { |
e9338405 AD |
189 | $running_jobs = count($children); |
190 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
ce1aa9b7 | 191 | } |
02008cb1 | 192 | |
dc24b520 | 193 | if ($last_checkpoint + $spawn_interval < time()) { |
02008cb1 | 194 | |
4c2da349 AD |
195 | /* Check if schema version changed */ |
196 | ||
6322ac79 | 197 | $test_schema_version = get_schema_version(); |
4c2da349 AD |
198 | |
199 | if ($test_schema_version != $schema_version) { | |
f0e015c4 AD |
200 | echo "Expected schema version: $schema_version, got: $test_schema_version\n"; |
201 | echo "Schema version changed while we were running, bailing out\n"; | |
4c2da349 AD |
202 | exit(100); |
203 | } | |
204 | ||
c90a028c | 205 | check_ctimes(); |
5a613536 AD |
206 | reap_children(); |
207 | ||
dc24b520 | 208 | for ($j = count($children); $j < $max_jobs; $j++) { |
02008cb1 AD |
209 | $pid = pcntl_fork(); |
210 | if ($pid == -1) { | |
211 | die("fork failed!\n"); | |
212 | } else if ($pid) { | |
724b7942 AD |
213 | |
214 | if (!$master_handlers_installed) { | |
215 | _debug("[MASTER] installing shutdown handlers"); | |
216 | pcntl_signal(SIGINT, 'sigint_handler'); | |
cfe6d444 | 217 | register_shutdown_function('shutdown', posix_getpid()); |
724b7942 AD |
218 | $master_handlers_installed = true; |
219 | } | |
220 | ||
e9338405 AD |
221 | _debug("[MASTER] spawned client $j [PID:$pid]..."); |
222 | array_push($children, $pid); | |
c90a028c | 223 | $ctimes[$pid] = time(); |
02008cb1 AD |
224 | } else { |
225 | pcntl_signal(SIGCHLD, SIG_IGN); | |
8ccaff02 AD |
226 | pcntl_signal(SIGINT, 'task_sigint_handler'); |
227 | ||
228 | register_shutdown_function('task_shutdown'); | |
229 | ||
7440a7fe | 230 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; |
8292d05b | 231 | |
7440a7fe | 232 | $my_pid = posix_getpid(); |
dbaa4e4a | 233 | |
7440a7fe | 234 | passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid"); |
a26f0c17 | 235 | |
ffa7cbae | 236 | sleep(1); |
8ccaff02 | 237 | |
ffa7cbae | 238 | // We exit in order to avoid fork bombing. |
02008cb1 AD |
239 | exit(0); |
240 | } | |
241 | } | |
242 | $last_checkpoint = time(); | |
243 | } | |
244 | sleep(1); | |
245 | } | |
246 | ||
247 | ?> |