]>
git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
3 set_include_path(dirname(__FILE__
) ."/include" . PATH_SEPARATOR
.
7 chdir(dirname(__FILE__
));
9 define('DISABLE_SESSIONS', true);
11 require_once "version.php";
12 require_once "autoload.php";
13 require_once "functions.php";
14 require_once "config.php";
17 define_default('PURGE_INTERVAL', 3600); // seconds
18 define_default('MAX_CHILD_RUNTIME', 1800); // seconds
19 define_default('MAX_JOBS', 2);
20 define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
); // seconds
22 require_once "sanity_check.php";
23 require_once "db.php";
24 require_once "db-prefs.php";
26 if (!function_exists('pcntl_fork')) {
27 die("error: This script requires PHP compiled with PCNTL module.\n");
30 $options = getopt("");
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");
40 $master_handlers_installed = false;
45 $last_checkpoint = -1;
48 * @SuppressWarnings(unused)
50 function reap_children() {
56 foreach ($children as $pid) {
57 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
59 if (file_is_locked("update_daemon-$pid.lock")) {
60 array_push($tmp, $pid);
62 Debug
::log("[reap_children] child $pid seems active but lockfile is unlocked.");
67 Debug
::log("[reap_children] child $pid reaped.");
77 function check_ctimes() {
80 foreach (array_keys($ctimes) as $pid) {
81 $started = $ctimes[$pid];
83 if (time() - $started > MAX_CHILD_RUNTIME
) {
84 Debug
::log("[MASTER] child process $pid seems to be stuck, aborting...");
85 posix_kill($pid, SIGKILL
);
91 * @SuppressWarnings(unused)
93 function sigchld_handler($signal) {
94 $running_jobs = reap_children();
96 Debug
::log("[SIGCHLD] jobs left: $running_jobs");
98 pcntl_waitpid(-1, $status, WNOHANG
);
101 function shutdown($caller_pid) {
102 if ($caller_pid == posix_getpid()) {
103 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock")) {
104 Debug
::log("removing lockfile (master)...");
105 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
110 function task_shutdown() {
111 $pid = posix_getpid();
113 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock")) {
114 Debug
::log("removing lockfile ($pid)...");
115 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
119 function sigint_handler() {
120 Debug
::log("[MASTER] SIG_INT received.\n");
121 shutdown(posix_getpid());
125 function task_sigint_handler() {
126 Debug
::log("[TASK] SIG_INT received.\n");
131 pcntl_signal(SIGCHLD
, 'sigchld_handler');
133 $longopts = array("log:",
140 $options = getopt("", $longopts);
142 if (isset($options["help"]) ) {
143 print "Tiny Tiny RSS update daemon.\n\n";
145 print " --log FILE - log messages to FILE\n";
146 print " --log-level N - log verbosity level\n";
147 print " --tasks N - amount of update tasks to spawn\n";
148 print " default: " . MAX_JOBS
. "\n";
149 print " --interval N - task spawn interval\n";
150 print " default: " . SPAWN_INTERVAL
. " seconds.\n";
151 print " --quiet - don't output messages to stdout\n";
155 Debug
::set_enabled(true);
156 Debug
::set_quiet(isset($options['quiet']));
158 if (isset($options["log-level"])) {
159 Debug
::set_loglevel((int)$options["log-level"]);
162 if (isset($options["log"])) {
163 Debug
::set_logfile($options["log"]);
164 Debug
::log("Logging to " . $options["log"]);
167 if (isset($options["tasks"])) {
168 Debug
::log("Set to spawn " . $options["tasks"] . " children.");
169 $max_jobs = $options["tasks"];
171 $max_jobs = MAX_JOBS
;
174 if (isset($options["interval"])) {
175 Debug
::log("Spawn interval: " . $options["interval"] . " seconds.");
176 $spawn_interval = $options["interval"];
178 $spawn_interval = SPAWN_INTERVAL
;
181 // let's enforce a minimum spawn interval as to not forkbomb the host
182 $spawn_interval = max(60, $spawn_interval);
183 Debug
::log("Spawn interval: $spawn_interval sec");
185 if (file_is_locked("update_daemon.lock")) {
186 die("error: Can't create lockfile. ".
187 "Maybe another daemon is already running.\n");
190 // Try to lock a file in order to avoid concurrent update.
191 $lock_handle = make_lockfile("update_daemon.lock");
194 die("error: Can't create lockfile. ".
195 "Maybe another daemon is already running.\n");
198 $schema_version = get_schema_version();
200 if ($schema_version != SCHEMA_VERSION
) {
201 die("Schema version is wrong, please upgrade the database.\n");
204 // Protip: children close shared database handle when terminating, it's a bad idea to
205 // do database stuff on main process from now on.
209 // Since sleep is interupted by SIGCHLD, we need another way to
210 // respect the spawn interval
211 $next_spawn = $last_checkpoint +
$spawn_interval - time();
213 if ($next_spawn %
60 == 0) {
214 $running_jobs = count($children);
215 Debug
::log("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
218 if ($last_checkpoint +
$spawn_interval < time()) {
222 for ($j = count($children); $j < $max_jobs; $j++
) {
225 die("fork failed!\n");
228 if (!$master_handlers_installed) {
229 Debug
::log("[MASTER] installing shutdown handlers");
230 pcntl_signal(SIGINT
, 'sigint_handler');
231 pcntl_signal(SIGTERM
, 'sigint_handler');
232 register_shutdown_function('shutdown', posix_getpid());
233 $master_handlers_installed = true;
236 Debug
::log("[MASTER] spawned client $j [PID:$pid]...");
237 array_push($children, $pid);
238 $ctimes[$pid] = time();
240 pcntl_signal(SIGCHLD
, SIG_IGN
);
241 pcntl_signal(SIGINT
, 'task_sigint_handler');
243 register_shutdown_function('task_shutdown');
245 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
246 $log = function_exists("flock") && isset($options['log']) ?
'--log '.$options['log'] : '';
248 $my_pid = posix_getpid();
250 passthru(PHP_EXECUTABLE
. " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
254 // We exit in order to avoid fork bombing.
258 $last_checkpoint = time();