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";
15 require_once "rssfuncs.php";
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
23 require_once "sanity_check.php";
24 require_once "db.php";
25 require_once "db-prefs.php";
27 if (!function_exists('pcntl_fork')) {
28 die("error: This script requires PHP compiled with PCNTL module.\n");
31 $options = getopt("");
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");
41 $master_handlers_installed = false;
46 $last_checkpoint = -1;
48 function reap_children() {
54 foreach ($children as $pid) {
55 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
57 if (file_is_locked("update_daemon-$pid.lock")) {
58 array_push($tmp, $pid);
60 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
65 _debug("[reap_children] child $pid reaped.");
75 function check_ctimes() {
78 foreach (array_keys($ctimes) as $pid) {
79 $started = $ctimes[$pid];
81 if (time() - $started > MAX_CHILD_RUNTIME) {
82 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
83 posix_kill($pid, SIGKILL);
88 function sigchld_handler($signal) {
89 $running_jobs = reap_children();
91 _debug("[SIGCHLD] jobs left: $running_jobs");
93 pcntl_waitpid(-1, $status, WNOHANG);
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");
105 function task_shutdown() {
106 $pid = posix_getpid();
108 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
109 _debug("removing lockfile ($pid)...");
110 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
114 function sigint_handler() {
115 _debug("[MASTER] SIG_INT received.\n");
116 shutdown(posix_getpid());
120 function task_sigint_handler() {
121 _debug("[TASK] SIG_INT received.\n");
126 pcntl_signal(SIGCHLD, 'sigchld_handler');
128 $longopts = array("log:",
134 $options = getopt("", $longopts);
136 if (isset($options["help"]) ) {
137 print "Tiny Tiny RSS update daemon.\n\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";
148 define('QUIET', isset($options['quiet']));
150 if (isset($options["tasks"])) {
151 _debug("Set to spawn " . $options["tasks"] . " children.");
152 $max_jobs = $options["tasks"];
154 $max_jobs = MAX_JOBS;
157 if (isset($options["interval"])) {
158 _debug("Spawn interval: " . $options["interval"] . " seconds.");
159 $spawn_interval = $options["interval"];
161 $spawn_interval = SPAWN_INTERVAL;
164 if (isset($options["log"])) {
165 _debug("Logging to " . $options["log"]);
166 define('LOGFILE', $options["log"]);
169 if (file_is_locked("update_daemon.lock")) {
170 die("error: Can't create lockfile. ".
171 "Maybe another daemon is already running.\n");
174 // Try to lock a file in order to avoid concurrent update.
175 $lock_handle = make_lockfile("update_daemon.lock");
178 die("error: Can't create lockfile. ".
179 "Maybe another daemon is already running.\n");
182 $schema_version = get_schema_version();
184 if ($schema_version != SCHEMA_VERSION) {
185 die("Schema version is wrong, please upgrade the database.\n");
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.
193 // Since sleep is interupted by SIGCHLD, we need another way to
194 // respect the spawn interval
195 $next_spawn = $last_checkpoint + $spawn_interval - time();
197 if ($next_spawn % 60 == 0) {
198 $running_jobs = count($children);
199 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
202 if ($last_checkpoint + $spawn_interval < time()) {
206 for ($j = count($children); $j < $max_jobs; $j++) {
209 die("fork failed!\n");
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;
220 _debug("[MASTER] spawned client $j [PID:$pid]...");
221 array_push($children, $pid);
222 $ctimes[$pid] = time();
224 pcntl_signal(SIGCHLD, SIG_IGN);
225 pcntl_signal(SIGINT, 'task_sigint_handler');
227 register_shutdown_function('task_shutdown');
229 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
230 $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
232 $my_pid = posix_getpid();
234 passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
238 // We exit in order to avoid fork bombing.
242 $last_checkpoint = time();