]>
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";
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;
49 * @SuppressWarnings(unused)
51 function reap_children() {
57 foreach ($children as $pid) {
58 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
60 if (file_is_locked("update_daemon-$pid.lock")) {
61 array_push($tmp, $pid);
63 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
68 _debug("[reap_children] child $pid reaped.");
78 function check_ctimes() {
81 foreach (array_keys($ctimes) as $pid) {
82 $started = $ctimes[$pid];
84 if (time() - $started > MAX_CHILD_RUNTIME
) {
85 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
86 posix_kill($pid, SIGKILL
);
92 * @SuppressWarnings(unused)
94 function sigchld_handler($signal) {
95 $running_jobs = reap_children();
97 _debug("[SIGCHLD] jobs left: $running_jobs");
99 pcntl_waitpid(-1, $status, WNOHANG
);
102 function shutdown($caller_pid) {
103 if ($caller_pid == posix_getpid()) {
104 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock")) {
105 _debug("removing lockfile (master)...");
106 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
111 function task_shutdown() {
112 $pid = posix_getpid();
114 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock")) {
115 _debug("removing lockfile ($pid)...");
116 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
120 function sigint_handler() {
121 _debug("[MASTER] SIG_INT received.\n");
122 shutdown(posix_getpid());
126 function task_sigint_handler() {
127 _debug("[TASK] SIG_INT received.\n");
132 pcntl_signal(SIGCHLD
, 'sigchld_handler');
134 $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 " --tasks N - amount of update tasks to spawn\n";
147 print " default: " . MAX_JOBS
. "\n";
148 print " --interval N - task spawn interval\n";
149 print " default: " . SPAWN_INTERVAL
. " seconds.\n";
150 print " --quiet - don't output messages to stdout\n";
154 define('QUIET', isset($options['quiet']));
156 if (isset($options["tasks"])) {
157 _debug("Set to spawn " . $options["tasks"] . " children.");
158 $max_jobs = $options["tasks"];
160 $max_jobs = MAX_JOBS
;
163 if (isset($options["interval"])) {
164 _debug("Spawn interval: " . $options["interval"] . " seconds.");
165 $spawn_interval = $options["interval"];
167 $spawn_interval = SPAWN_INTERVAL
;
170 if (isset($options["log"])) {
171 _debug("Logging to " . $options["log"]);
172 define('LOGFILE', $options["log"]);
175 if (file_is_locked("update_daemon.lock")) {
176 die("error: Can't create lockfile. ".
177 "Maybe another daemon is already running.\n");
180 // Try to lock a file in order to avoid concurrent update.
181 $lock_handle = make_lockfile("update_daemon.lock");
184 die("error: Can't create lockfile. ".
185 "Maybe another daemon is already running.\n");
188 $schema_version = get_schema_version();
190 if ($schema_version != SCHEMA_VERSION
) {
191 die("Schema version is wrong, please upgrade the database.\n");
194 // Protip: children close shared database handle when terminating, it's a bad idea to
195 // do database stuff on main process from now on.
199 // Since sleep is interupted by SIGCHLD, we need another way to
200 // respect the spawn interval
201 $next_spawn = $last_checkpoint +
$spawn_interval - time();
203 if ($next_spawn %
60 == 0) {
204 $running_jobs = count($children);
205 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
208 if ($last_checkpoint +
$spawn_interval < time()) {
212 for ($j = count($children); $j < $max_jobs; $j++
) {
215 die("fork failed!\n");
218 if (!$master_handlers_installed) {
219 _debug("[MASTER] installing shutdown handlers");
220 pcntl_signal(SIGINT
, 'sigint_handler');
221 pcntl_signal(SIGTERM
, 'sigint_handler');
222 register_shutdown_function('shutdown', posix_getpid());
223 $master_handlers_installed = true;
226 _debug("[MASTER] spawned client $j [PID:$pid]...");
227 array_push($children, $pid);
228 $ctimes[$pid] = time();
230 pcntl_signal(SIGCHLD
, SIG_IGN
);
231 pcntl_signal(SIGINT
, 'task_sigint_handler');
233 register_shutdown_function('task_shutdown');
235 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
236 $log = function_exists("flock") && isset($options['log']) ?
'--log '.$options['log'] : '';
238 $my_pid = posix_getpid();
240 passthru(PHP_EXECUTABLE
. " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
244 // We exit in order to avoid fork bombing.
248 $last_checkpoint = time();