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