]>
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 "functions.php";
18 require_once "rssfuncs.php";
19 require_once "sanity_check.php";
20 require_once "config.php";
21 require_once "db.php";
22 require_once "db-prefs.php";
25 define('PURGE_INTERVAL', 3600); // seconds
26 define('MAX_CHILD_RUNTIME', 600); // seconds
27 define('MAX_JOBS', 2);
28 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
); // seconds
30 if (!function_exists('pcntl_fork')) {
31 die("error: This script requires PHP compiled with PCNTL module.\n");
34 $master_handlers_installed = false;
39 $last_checkpoint = -1;
41 function reap_children() {
47 foreach ($children as $pid) {
48 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
50 if (file_is_locked("update_daemon-$pid.lock")) {
51 array_push($tmp, $pid);
53 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
58 _debug("[reap_children] child $pid reaped.");
68 function check_ctimes() {
71 foreach (array_keys($ctimes) as $pid) {
72 $started = $ctimes[$pid];
74 if (time() - $started > MAX_CHILD_RUNTIME
) {
75 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
76 posix_kill($pid, SIGKILL
);
81 function sigchld_handler($signal) {
82 $running_jobs = reap_children();
84 _debug("[SIGCHLD] jobs left: $running_jobs");
86 pcntl_waitpid(-1, $status, WNOHANG
);
89 function shutdown($caller_pid) {
90 if ($caller_pid == posix_getpid()) {
91 if (file_exists(LOCK_DIRECTORY
. "/update_daemon.lock")) {
92 _debug("removing lockfile (master)...");
93 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
98 function task_shutdown() {
99 $pid = posix_getpid();
101 if (file_exists(LOCK_DIRECTORY
. "/update_daemon-$pid.lock")) {
102 _debug("removing lockfile ($pid)...");
103 unlink(LOCK_DIRECTORY
. "/update_daemon-$pid.lock");
107 function sigint_handler() {
108 _debug("[MASTER] SIG_INT received.\n");
109 shutdown(posix_getpid());
113 function task_sigint_handler() {
114 _debug("[TASK] SIG_INT received.\n");
119 pcntl_signal(SIGCHLD
, 'sigchld_handler');
121 $longopts = array("log:",
127 $options = getopt("", $longopts);
129 if (isset($options["help"]) ) {
130 print "Tiny Tiny RSS update daemon.\n\n";
132 print " --log FILE - log messages to FILE\n";
133 print " --tasks N - amount of update tasks to spawn\n";
134 print " default: " . MAX_JOBS
. "\n";
135 print " --interval N - task spawn interval\n";
136 print " default: " . SPAWN_INTERVAL
. " seconds.\n";
137 print " --quiet - don't output messages to stdout\n";
141 define('QUIET', isset($options['quiet']));
143 if (isset($options["tasks"])) {
144 _debug("Set to spawn " . $options["tasks"] . " children.");
145 $max_jobs = $options["tasks"];
147 $max_jobs = MAX_JOBS
;
150 if (isset($options["interval"])) {
151 _debug("Spawn interval: " . $options["interval"] . " seconds.");
152 $spawn_interval = $options["interval"];
154 $spawn_interval = SPAWN_INTERVAL
;
157 if (isset($options["log"])) {
158 _debug("Logging to " . $options["log"]);
159 define('LOGFILE', $options["log"]);
162 if (file_is_locked("update_daemon.lock")) {
163 die("error: Can't create lockfile. ".
164 "Maybe another daemon is already running.\n");
167 // Try to lock a file in order to avoid concurrent update.
168 $lock_handle = make_lockfile("update_daemon.lock");
171 die("error: Can't create lockfile. ".
172 "Maybe another daemon is already running.\n");
175 // Testing database connection.
176 // It is unnecessary to start the fork loop if database is not ok.
177 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
179 if (!init_connection($link)) die("Can't initialize db connection.\n");
181 $schema_version = get_schema_version($link);
185 if ($schema_version != SCHEMA_VERSION
) {
186 die("Schema version is wrong, please upgrade the database.\n");
191 // Since sleep is interupted by SIGCHLD, we need another way to
192 // respect the spawn interval
193 $next_spawn = $last_checkpoint +
$spawn_interval - time();
195 if ($next_spawn %
60 == 0) {
196 $running_jobs = count($children);
197 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
200 if ($last_checkpoint +
$spawn_interval < time()) {
202 /* Check if schema version changed */
204 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
205 if (!init_connection($link)) die("Can't initialize db connection.\n");
206 $test_schema_version = get_schema_version($link);
209 if ($test_schema_version != $schema_version) {
210 echo "Expected schema version: $schema_version, got: $test_schema_version\n";
211 echo "Schema version changed while we were running, bailing out\n";
218 for ($j = count($children); $j < $max_jobs; $j++
) {
221 die("fork failed!\n");
224 if (!$master_handlers_installed) {
225 _debug("[MASTER] installing shutdown handlers");
226 pcntl_signal(SIGINT
, 'sigint_handler');
227 register_shutdown_function('shutdown', posix_getpid());
228 $master_handlers_installed = true;
231 _debug("[MASTER] spawned client $j [PID:$pid]...");
232 array_push($children, $pid);
233 $ctimes[$pid] = time();
235 pcntl_signal(SIGCHLD
, SIG_IGN
);
236 pcntl_signal(SIGINT
, 'task_sigint_handler');
238 register_shutdown_function('task_shutdown');
240 $my_pid = posix_getpid();
241 $lock_filename = "update_daemon-$my_pid.lock";
243 $lock_handle = make_lockfile($lock_filename);
246 die("error: Can't create lockfile ($lock_filename). ".
247 "Maybe another daemon is already running.\n");
250 // ****** Updating RSS code *******
251 // Only run in fork process.
253 $start_timestamp = time();
255 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
257 if (!init_connection($link)) return;
259 // We disable stamp file, since it is of no use in a multiprocess update.
260 // not really, tho for the time being -fox
261 if (!make_stampfile('update_daemon.stamp')) {
262 _debug("warning: unable to create stampfile\n");
265 // Call to the feed batch update function
266 // and maybe regenerate feedbrowser cache
270 _debug("Waiting before update [$j]..");
272 $nf = update_daemon_common($link);
274 if (rand(0,100) > 50) {
275 $count = update_feedbrowser_cache($link);
276 _debug("Feedbrowser updated, $count feeds processed.");
278 purge_orphans($link, true);
280 $rc = cleanup_tags($link, 14, 50000);
282 _debug("Cleaned $rc cached tags.");
285 $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK
, "hook_update_task", $op);
288 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
291 _debug("Feeds processed: $nf");
293 if (time() - $start_timestamp > 0) {
294 _debug("Feeds/minute: " . sprintf("%.2d", $nf/((time()-$start_timestamp)/60)));
301 // We wait a little before exiting to avoid to be faster than our parent process.
304 unlink(LOCK_DIRECTORY
. "/$lock_filename");
306 // We exit in order to avoid fork bombing.
310 $last_checkpoint = time();