Added a timer to run repository checks while the browser is up

Added a separate retry interval so that the extension retries sooner after failures (browser offline, request failure, etc.)

Revision 200 -- w00t i am victorious
This commit is contained in:
Dan Stillman 2006-06-15 21:06:24 +00:00
parent 2d20fe717d
commit 8e97675cc9
2 changed files with 58 additions and 10 deletions

View file

@ -1,10 +1,11 @@
Scholar.Schema = new function(){ Scholar.Schema = new function(){
var _dbVersions = []; var _dbVersions = [];
var _schemaVersions = []; var _schemaVersions = [];
var _repositoryTimer;
this.updateSchema = updateSchema; this.updateSchema = updateSchema;
this.updateScrapersRemote = updateScrapersRemote; this.updateScrapersRemote = updateScrapersRemote;
this.stopRepositoryTimer = stopRepositoryTimer;
/* /*
* Checks if the DB schema exists and is up-to-date, updating if necessary * Checks if the DB schema exists and is up-to-date, updating if necessary
@ -50,14 +51,16 @@ Scholar.Schema = new function(){
**/ **/
function updateScrapersRemote(force){ function updateScrapersRemote(force){
// Determine the earliest local time that we'd query the repository again // Determine the earliest local time that we'd query the repository again
var lastChecked = _getDBVersion('lastcheck'); var nextCheck = new Date();
var d = new Date(); nextCheck.setTime((parseInt(_getDBVersion('lastcheck'))
d.setTime((parseInt(lastChecked)
+ SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL']) * 1000); // JS uses ms + SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL']) * 1000); // JS uses ms
var now = new Date();
// If enough time hasn't passed and it's not being forced, don't update // If enough time hasn't passed and it's not being forced, don't update
if (!force && new Date() < d){ if (!force && now < nextCheck){
Scholar.debug('Not checking repository', 4); Scholar.debug('Too soon since last update -- not checking repository', 4);
// Set the repository timer to the remaining time
_setRepositoryTimer(Math.round((nextCheck.getTime() - now.getTime()) / 1000));
return false; return false;
} }
@ -69,8 +72,23 @@ Scholar.Schema = new function(){
+ 'version=' + Scholar.version; + 'version=' + Scholar.version;
Scholar.debug('Checking repository for updates (' + url + ')'); Scholar.debug('Checking repository for updates (' + url + ')');
Scholar.HTTP.doGet(url, false, _updateScrapersRemoteCallback); var get = Scholar.HTTP.doGet(url, false, _updateScrapersRemoteCallback);
// TODO: instead, add an observer to start and stop timer on online state change
if (!get){
Scholar.debug('Browser is offline -- skipping check');
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_CHECK_RETRY']);
} }
}
function stopRepositoryTimer(){
if (_repositoryTimer){
Scholar.debug('Stopping repository check timer');
_repositoryTimer.cancel();
}
}
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
@ -270,6 +288,7 @@ Scholar.Schema = new function(){
if (!updates.length){ if (!updates.length){
Scholar.debug('All scrapers are up-to-date'); Scholar.debug('All scrapers are up-to-date');
Scholar.DB.commitTransaction(); Scholar.DB.commitTransaction();
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL']);
return false; return false;
} }
@ -287,6 +306,35 @@ Scholar.Schema = new function(){
if (!breakout){ if (!breakout){
Scholar.DB.commitTransaction(); Scholar.DB.commitTransaction();
_setRepositoryTimer(SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL']);
}
}
/**
* Set the interval between repository queries
*
* We add an additional two seconds to avoid race conditions
**/
function _setRepositoryTimer(interval){
if (!interval){
interval = SCHOLAR_CONFIG['REPOSITORY_CHECK_INTERVAL'];
}
var fudge = 2; // two seconds
var displayInterval = interval + fudge;
var interval = (interval + fudge) * 1000; // convert to ms
if (!_repositoryTimer || _repositoryTimer.delay!=interval){
Scholar.debug('Setting repository check interval to ' + displayInterval + ' seconds');
_repositoryTimer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
_repositoryTimer.initWithCallback({
// implements nsITimerCallback
notify: function(timer){
Scholar.Schema.updateScrapersRemote();
}
}, interval, Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
} }
} }
@ -315,7 +363,6 @@ Scholar.Schema = new function(){
} }
/* /*
* Migrate schema from an older version, preserving data * Migrate schema from an older version, preserving data
*/ */

View file

@ -5,7 +5,8 @@ const SCHOLAR_CONFIG = {
DEBUG_LOGGING: true, DEBUG_LOGGING: true,
DEBUG_TO_CONSOLE: true, // dump debug messages to console rather than (much slower) Debug Logger DEBUG_TO_CONSOLE: true, // dump debug messages to console rather than (much slower) Debug Logger
REPOSITORY_URL: 'http://chnm.gmu.edu/firefoxscholar/repo', REPOSITORY_URL: 'http://chnm.gmu.edu/firefoxscholar/repo',
REPOSITORY_CHECK_INTERVAL: 86400 // 24 hours REPOSITORY_CHECK_INTERVAL: 86400, // 24 hours
REPOSITORY_CHECK_RETRY: 60 // 1 hour
}; };
/* /*