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 "config.php";
13 require_once "autoload.php";
14 require_once "functions.php";
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
22 require_once "rssfuncs.php";
23 require_once "sanity_check.php";
24 require_once "db.php";
25 require_once "db-prefs.php";
28 if (!function_exists('pcntl_fork')) {
29 die("error: This script requires PHP compiled with PCNTL module.\n");
32 $master_handlers_installed = false;
37 $last_checkpoint = -1;
39 function reap_children() {
45 foreach ($children as $pid) {
46 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
48 if (file_is_locked("update_daemon-$pid.lock")) {
49 array_push($tmp, $pid);
51 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
56 _debug("[reap_children] child $pid reaped.");
66 function check_ctimes() {
69 foreach (array_keys($ctimes) as $pid) {
70 $started = $ctimes[$pid];
72 if (time() - $started > MAX_CHILD_RUNTIME) {
73 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
74 posix_kill($pid, SIGKILL);
79 function sigchld_handler($signal) {
80 $running_jobs = reap_children();
82 _debug("[SIGCHLD] jobs left: $running_jobs");
84 pcntl_waitpid(-1, $status, WNOHANG);
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");
96 function task_shutdown() {
97 $pid = posix_getpid();
99 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
100 _debug("removing lockfile ($pid)...");
101 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
105 function sigint_handler() {
106 _debug("[MASTER] SIG_INT received.\n");
107 shutdown(posix_getpid());
111 function task_sigint_handler() {
112 _debug("[TASK] SIG_INT received.\n");
117 pcntl_signal(SIGCHLD, 'sigchld_handler');
119 $longopts = array("log:",
125 $options = getopt("", $longopts);
127 if (isset($options["help"]) ) {
128 print "Tiny Tiny RSS update daemon.\n\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";
139 define('QUIET', isset($options['quiet']));
141 if (isset($options["tasks"])) {
142 _debug("Set to spawn " . $options["tasks"] . " children.");
143 $max_jobs = $options["tasks"];
145 $max_jobs = MAX_JOBS;
148 if (isset($options["interval"])) {
149 _debug("Spawn interval: " . $options["interval"] . " seconds.");
150 $spawn_interval = $options["interval"];
152 $spawn_interval = SPAWN_INTERVAL;
155 if (isset($options["log"])) {
156 _debug("Logging to " . $options["log"]);
157 define('LOGFILE', $options["log"]);
160 if (file_is_locked("update_daemon.lock")) {
161 die("error: Can't create lockfile. ".
162 "Maybe another daemon is already running.\n");
165 // Try to lock a file in order to avoid concurrent update.
166 $lock_handle = make_lockfile("update_daemon.lock");
169 die("error: Can't create lockfile. ".
170 "Maybe another daemon is already running.\n");
175 $schema_version = get_schema_version();
177 if ($schema_version != SCHEMA_VERSION) {
178 die("Schema version is wrong, please upgrade the database.\n");
183 // Since sleep is interupted by SIGCHLD, we need another way to
184 // respect the spawn interval
185 $next_spawn = $last_checkpoint + $spawn_interval - time();
187 if ($next_spawn % 60 == 0) {
188 $running_jobs = count($children);
189 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
192 if ($last_checkpoint + $spawn_interval < time()) {
194 /* Check if schema version changed */
196 $test_schema_version = get_schema_version();
198 if ($test_schema_version != $schema_version) {
199 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
200 echo "Schema version changed while we were running, bailing out\n";
207 for ($j = count($children); $j < $max_jobs; $j++) {
210 die("fork failed!\n");
213 if (!$master_handlers_installed) {
214 _debug("[MASTER] installing shutdown handlers");
215 pcntl_signal(SIGINT, '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" : "";
231 $my_pid = posix_getpid();
233 passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
237 // We exit in order to avoid fork bombing.
241 $last_checkpoint = time();