]>
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";
13 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
14 define('DAEMON_EXTENDED_DEBUG', true);
17 require_once "autoload.php";
18 require_once "functions.php";
19 require_once "rssfuncs.php";
20 require_once "sanity_check.php";
21 require_once "config.php";
22 require_once "db.php";
23 require_once "db-prefs.php";
26 define('PURGE_INTERVAL', 3600); // seconds
27 define('MAX_CHILD_RUNTIME', 600); // seconds
28 define('MAX_JOBS', 2);
29 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
); // seconds
31 if (!function_exists('pcntl_fork')) {
32 die("error: This script requires PHP compiled with PCNTL module.\n");
35 $master_handlers_installed = false;
40 $last_checkpoint = -1;
42 function reap_children() {
48 foreach ($children as $pid) {
49 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
51 if (file_is_locked("update_daemon-$pid.lock")) {
52 array_push($tmp, $pid);
54 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
59 _debug("[reap_children] child $pid reaped.");
69 function check_ctimes() {
72 foreach (array_keys($ctimes) as $pid) {
73 $started = $ctimes[$pid];
75 if (time() - $started > MAX_CHILD_RUNTIME
) {
76 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
77 posix_kill($pid, SIGKILL
);
82 function sigchld_handler($signal) {
83 $running_jobs = reap_children();
85 _debug("[SIGCHLD] jobs left: $running_jobs");
87 pcntl_waitpid(-1, $status, WNOHANG
);
90 function shutdown($caller_pid) {
91 if ($caller_pid == posix_getpid()) {
92 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock")) {
93 _debug("removing lockfile (master)...");
94 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
99 function task_shutdown() {
100 $pid = posix_getpid();
102 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock")) {
103 _debug("removing lockfile ($pid)...");
104 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
108 function sigint_handler() {
109 _debug("[MASTER] SIG_INT received.\n");
110 shutdown(posix_getpid());
114 function task_sigint_handler() {
115 _debug("[TASK] SIG_INT received.\n");
120 pcntl_signal(SIGCHLD
, 'sigchld_handler');
122 $longopts = array("log:",
128 $options = getopt("", $longopts);
130 if (isset($options["help"]) ) {
131 print "Tiny Tiny RSS update daemon.\n\n";
133 print " --log FILE - log messages to FILE\n";
134 print " --tasks N - amount of update tasks to spawn\n";
135 print " default: " . MAX_JOBS
. "\n";
136 print " --interval N - task spawn interval\n";
137 print " default: " . SPAWN_INTERVAL
. " seconds.\n";
138 print " --quiet - don't output messages to stdout\n";
142 define('QUIET', isset($options['quiet']));
144 if (isset($options["tasks"])) {
145 _debug("Set to spawn " . $options["tasks"] . " children.");
146 $max_jobs = $options["tasks"];
148 $max_jobs = MAX_JOBS
;
151 if (isset($options["interval"])) {
152 _debug("Spawn interval: " . $options["interval"] . " seconds.");
153 $spawn_interval = $options["interval"];
155 $spawn_interval = SPAWN_INTERVAL
;
158 if (isset($options["log"])) {
159 _debug("Logging to " . $options["log"]);
160 define('LOGFILE', $options["log"]);
163 if (file_is_locked("update_daemon.lock")) {
164 die("error: Can't create lockfile. ".
165 "Maybe another daemon is already running.\n");
168 // Try to lock a file in order to avoid concurrent update.
169 $lock_handle = make_lockfile("update_daemon.lock");
172 die("error: Can't create lockfile. ".
173 "Maybe another daemon is already running.\n");
178 $schema_version = get_schema_version();
180 if ($schema_version != SCHEMA_VERSION
) {
181 die("Schema version is wrong, please upgrade the database.\n");
186 // Since sleep is interupted by SIGCHLD, we need another way to
187 // respect the spawn interval
188 $next_spawn = $last_checkpoint +
$spawn_interval - time();
190 if ($next_spawn %
60 == 0) {
191 $running_jobs = count($children);
192 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
195 if ($last_checkpoint +
$spawn_interval < time()) {
197 /* Check if schema version changed */
199 $test_schema_version = get_schema_version();
201 if ($test_schema_version != $schema_version) {
202 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
203 echo "Schema version changed while we were running, bailing out\n";
210 for ($j = count($children); $j < $max_jobs; $j++
) {
213 die("fork failed!\n");
216 if (!$master_handlers_installed) {
217 _debug("[MASTER] installing shutdown handlers");
218 pcntl_signal(SIGINT
, 'sigint_handler');
219 register_shutdown_function('shutdown', posix_getpid());
220 $master_handlers_installed = true;
223 _debug("[MASTER] spawned client $j [PID:$pid]...");
224 array_push($children, $pid);
225 $ctimes[$pid] = time();
227 pcntl_signal(SIGCHLD
, SIG_IGN
);
228 pcntl_signal(SIGINT
, 'task_sigint_handler');
230 register_shutdown_function('task_shutdown');
232 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
234 $my_pid = posix_getpid();
236 passthru(PHP_EXECUTABLE
. " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
240 // We exit in order to avoid fork bombing.
244 $last_checkpoint = time();