]>
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 "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', 1800); // seconds
19 define_default('MAX_JOBS', 2);
20 define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
); // seconds
21 define_default('DAEMON_FEED_LIMIT', 250);
23 require_once "rssfuncs.php";
24 require_once "sanity_check.php";
25 require_once "db.php";
26 require_once "db-prefs.php";
29 if (!function_exists('pcntl_fork')) {
30 die("error: This script requires PHP compiled with PCNTL module.\n");
33 $master_handlers_installed = false;
38 $last_checkpoint = -1;
40 function reap_children() {
46 foreach ($children as $pid) {
47 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
49 if (file_is_locked("update_daemon-$pid.lock")) {
50 array_push($tmp, $pid);
52 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
57 _debug("[reap_children] child $pid reaped.");
67 function check_ctimes() {
70 foreach (array_keys($ctimes) as $pid) {
71 $started = $ctimes[$pid];
73 if (time() - $started > MAX_CHILD_RUNTIME
) {
74 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
75 posix_kill($pid, SIGKILL
);
80 function sigchld_handler($signal) {
81 $running_jobs = reap_children();
83 _debug("[SIGCHLD] jobs left: $running_jobs");
85 pcntl_waitpid(-1, $status, WNOHANG
);
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");
97 function task_shutdown() {
98 $pid = posix_getpid();
100 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock")) {
101 _debug("removing lockfile ($pid)...");
102 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
106 function sigint_handler() {
107 _debug("[MASTER] SIG_INT received.\n");
108 shutdown(posix_getpid());
112 function task_sigint_handler() {
113 _debug("[TASK] SIG_INT received.\n");
118 pcntl_signal(SIGCHLD
, 'sigchld_handler');
120 $longopts = array("log:",
126 $options = getopt("", $longopts);
128 if (isset($options["help"]) ) {
129 print "Tiny Tiny RSS update daemon.\n\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";
140 define('QUIET', isset($options['quiet']));
142 if (isset($options["tasks"])) {
143 _debug("Set to spawn " . $options["tasks"] . " children.");
144 $max_jobs = $options["tasks"];
146 $max_jobs = MAX_JOBS
;
149 if (isset($options["interval"])) {
150 _debug("Spawn interval: " . $options["interval"] . " seconds.");
151 $spawn_interval = $options["interval"];
153 $spawn_interval = SPAWN_INTERVAL
;
156 if (isset($options["log"])) {
157 _debug("Logging to " . $options["log"]);
158 define('LOGFILE', $options["log"]);
161 if (file_is_locked("update_daemon.lock")) {
162 die("error: Can't create lockfile. ".
163 "Maybe another daemon is already running.\n");
166 // Try to lock a file in order to avoid concurrent update.
167 $lock_handle = make_lockfile("update_daemon.lock");
170 die("error: Can't create lockfile. ".
171 "Maybe another daemon is already running.\n");
176 $schema_version = get_schema_version();
178 if ($schema_version != SCHEMA_VERSION
) {
179 die("Schema version is wrong, please upgrade the database.\n");
184 // Since sleep is interupted by SIGCHLD, we need another way to
185 // respect the spawn interval
186 $next_spawn = $last_checkpoint +
$spawn_interval - time();
188 if ($next_spawn %
60 == 0) {
189 $running_jobs = count($children);
190 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
193 if ($last_checkpoint +
$spawn_interval < time()) {
195 /* Check if schema version changed */
197 $test_schema_version = get_schema_version();
199 if ($test_schema_version != $schema_version) {
200 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
201 echo "Schema version changed while we were running, bailing out\n";
208 for ($j = count($children); $j < $max_jobs; $j++
) {
211 die("fork failed!\n");
214 if (!$master_handlers_installed) {
215 _debug("[MASTER] installing shutdown handlers");
216 pcntl_signal(SIGINT
, 'sigint_handler');
217 register_shutdown_function('shutdown', posix_getpid());
218 $master_handlers_installed = true;
221 _debug("[MASTER] spawned client $j [PID:$pid]...");
222 array_push($children, $pid);
223 $ctimes[$pid] = time();
225 pcntl_signal(SIGCHLD
, SIG_IGN
);
226 pcntl_signal(SIGINT
, 'task_sigint_handler');
228 register_shutdown_function('task_shutdown');
230 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
232 $my_pid = posix_getpid();
234 passthru(PHP_EXECUTABLE
. " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
238 // We exit in order to avoid fork bombing.
242 $last_checkpoint = time();