]>
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";
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";
28 if (!function_exists('pcntl_fork')) {
29 die("error: This script requires PHP compiled with PCNTL module.\n");
35 $last_checkpoint = -1;
37 function reap_children() {
43 foreach ($children as $pid) {
44 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
46 if (file_is_locked("update_daemon-$pid.lock")) {
47 array_push($tmp, $pid);
49 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
54 _debug("[reap_children] child $pid reaped.");
64 function check_ctimes() {
67 foreach (array_keys($ctimes) as $pid) {
68 $started = $ctimes[$pid];
70 if (time() - $started > MAX_CHILD_RUNTIME
) {
71 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
72 posix_kill($pid, SIGKILL
);
77 function sigchld_handler($signal) {
78 $running_jobs = reap_children();
80 _debug("[SIGCHLD] jobs left: $running_jobs");
82 pcntl_waitpid(-1, $status, WNOHANG
);
85 pcntl_signal(SIGCHLD
, 'sigchld_handler');
87 $longopts = array("log:",
93 $options = getopt("", $longopts);
95 if (isset($options["help"]) ) {
96 print "Tiny Tiny RSS update daemon.\n\n";
98 print " --log FILE - log messages to FILE\n";
99 print " --tasks N - amount of update tasks to spawn\n";
100 print " default: " . MAX_JOBS
. "\n";
101 print " --interval N - task spawn interval\n";
102 print " default: " . SPAWN_INTERVAL
. " seconds.\n";
103 print " --quiet - don't output messages to stdout\n";
107 define('QUIET', isset($options['quiet']));
109 if (isset($options["tasks"])) {
110 _debug("Set to spawn " . $options["tasks"] . " children.");
111 $max_jobs = $options["tasks"];
113 $max_jobs = MAX_JOBS
;
116 if (isset($options["interval"])) {
117 _debug("Spawn interval: " . $options["interval"] . " seconds.");
118 $spawn_interval = $options["interval"];
120 $spawn_interval = SPAWN_INTERVAL
;
123 if (isset($options["log"])) {
124 _debug("Logging to " . $options["log"]);
125 define('LOGFILE', $options["log"]);
128 if (file_is_locked("update_daemon.lock")) {
129 die("error: Can't create lockfile. ".
130 "Maybe another daemon is already running.\n");
133 // Try to lock a file in order to avoid concurrent update.
134 $lock_handle = make_lockfile("update_daemon.lock");
137 die("error: Can't create lockfile. ".
138 "Maybe another daemon is already running.\n");
141 $schema_version = get_schema_version();
143 if ($schema_version != SCHEMA_VERSION
) {
144 die("Schema version is wrong, please upgrade the database.\n");
147 // Protip: children close shared database handle when terminating, it's a bad idea to
148 // do database stuff on main process from now on.
152 // Since sleep is interupted by SIGCHLD, we need another way to
153 // respect the spawn interval
154 $next_spawn = $last_checkpoint +
$spawn_interval - time();
156 if ($next_spawn %
60 == 0) {
157 $running_jobs = count($children);
158 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
161 if ($last_checkpoint +
$spawn_interval < time()) {
165 for ($j = count($children); $j < $max_jobs; $j++
) {
168 die("fork failed!\n");
170 _debug("[MASTER] spawned client $j [PID:$pid]...");
171 array_push($children, $pid);
172 $ctimes[$pid] = time();
174 pcntl_signal(SIGCHLD
, SIG_IGN
);
176 $quiet = (isset($options["quiet"])) ?
"--quiet" : "";
178 $my_pid = posix_getpid();
180 passthru(PHP_EXECUTABLE
. " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
184 // We exit in order to avoid fork bombing.
188 $last_checkpoint = time();