comparison modules/service.js @ 357:1db123f46ed7

add some glue to let sources be queried to see if they are also targets
author Myk Melez <myk@mozilla.org>
date Mon, 03 Nov 2008 18:02:27 -0800
parents e9d7087abad1
children c00b3db58dcf
comparison
equal deleted inserted replaced
356:8d90feea857c 357:1db123f46ed7
39 const Cc = Components.classes; 39 const Cc = Components.classes;
40 const Ci = Components.interfaces; 40 const Ci = Components.interfaces;
41 const Cr = Components.results; 41 const Cr = Components.results;
42 const Cu = Components.utils; 42 const Cu = Components.utils;
43 43
44 // modules that come with Firefox
44 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 45 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
46
47 // modules that are generic
45 Cu.import("resource://snowl/modules/log4moz.js"); 48 Cu.import("resource://snowl/modules/log4moz.js");
49 Cu.import("resource://snowl/modules/URI.js");
50
51 // modules that are Snowl-specific
46 Cu.import("resource://snowl/modules/datastore.js"); 52 Cu.import("resource://snowl/modules/datastore.js");
47 Cu.import("resource://snowl/modules/feed.js"); 53 Cu.import("resource://snowl/modules/feed.js");
48 Cu.import("resource://snowl/modules/twitter.js"); 54 Cu.import("resource://snowl/modules/twitter.js");
49 Cu.import("resource://snowl/modules/source.js"); 55 Cu.import("resource://snowl/modules/source.js");
50 Cu.import("resource://snowl/modules/URI.js"); 56 Cu.import("resource://snowl/modules/target.js");
51 Cu.import("resource://snowl/modules/utils.js"); 57 Cu.import("resource://snowl/modules/utils.js");
52 58
53 const PERMS_FILE = 0644; 59 const PERMS_FILE = 0644;
54 const PERMS_DIRECTORY = 0755; 60 const PERMS_DIRECTORY = 0755;
55 61
212 this._prefSvc.savePrefFile(null); 218 this._prefSvc.savePrefFile(null);
213 } 219 }
214 } 220 }
215 }, 221 },
216 222
217 get _getSourcesStatement() { 223 get _getAccountsStatement() {
218 let statement = SnowlDatastore.createStatement( 224 delete this._getAccountsStatement;
225 return this._getAccountsStatement = SnowlDatastore.createStatement(
219 "SELECT id, type, name, machineURI, humanURI, lastRefreshed, importance FROM sources" 226 "SELECT id, type, name, machineURI, humanURI, lastRefreshed, importance FROM sources"
220 ); 227 );
221 delete this._getSourcesStatement; 228 },
222 this._getSourcesStatement = statement; 229
223 return this._getSourcesStatement; 230 get accounts() {
224 }, 231 let accounts = [];
225
226 getSources: function() {
227 let sources = [];
228 232
229 try { 233 try {
230 while (this._getSourcesStatement.step()) { 234 while (this._getAccountsStatement.step()) {
231 let row = this._getSourcesStatement.row; 235 let row = this._getAccountsStatement.row;
232 236
233 let constructor = SnowlSource; 237 let type;
234 try { 238 try {
235 constructor = eval(row.type); 239 type = eval(row.type);
240 this._log.info("got " + row.type + " for " + row.name);
236 } 241 }
237 catch(ex) { 242 catch(ex) {
238 this._log.warn("error evaling " + row.type + ": " + ex + 243 this._log.error("error getting " + row.name + ": " + ex);
239 "; falling back to SnowlSource"); 244 continue;
240 } 245 }
241 246
242 sources.push(new constructor(row.id, 247 accounts.push(new type(row.id,
243 row.name, 248 row.name,
244 URI.get(row.machineURI), 249 URI.get(row.machineURI),
245 URI.get(row.humanURI), 250 URI.get(row.humanURI),
246 SnowlDateUtils.julianToJSDate(row.lastRefreshed), 251 SnowlDateUtils.julianToJSDate(row.lastRefreshed),
247 row.importance)); 252 row.importance));
248 } 253 }
249 } 254 }
250 finally { 255 finally {
251 this._getSourcesStatement.reset(); 256 this._getAccountsStatement.reset();
252 } 257 }
253 258
254 return sources; 259 return accounts;
260 },
261
262 get sources() {
263 return this.accounts.filter(function(acct) acct.implements(SnowlSource));
264 },
265
266 get targets() {
267 return this.accounts.filter(function(acct) acct.implements(SnowlTarget));
255 }, 268 },
256 269
257 refreshStaleSources: function() { 270 refreshStaleSources: function() {
258 this._log.info("refreshing stale sources"); 271 this._log.info("refreshing stale sources");
259 272
260 // XXX Should SnowlDatastore::selectSources return SnowlSource objects,
261 // of which SnowlFeed is a subclass? Or perhaps selectSources should simply
262 // return a database cursor, and SnowlService::getSources should return
263 // SnowlSource objects?
264 let allSources = this.getSources();
265 let now = new Date(); 273 let now = new Date();
266 let staleSources = []; 274 let staleSources = [];
267 for each (let source in allSources) { 275 for each (let source in this.sources)
268 //this._log.info(source.name + " last refreshed " + source.lastRefreshed + ", " + (now - source.lastRefreshed)/1000 + "s ago; interval is " + source.refreshInterval/1000 + "s"); 276 if (now - source.lastRefreshed > source.refreshInterval)
269 if (now - source.lastRefreshed > source.refreshInterval) {
270 this._log.info("source: " + source.id + " is stale; refreshing");
271 staleSources.push(source); 277 staleSources.push(source);
272 }
273 }
274 this._refreshSources(staleSources); 278 this._refreshSources(staleSources);
275 }, 279 },
276 280
277 refreshAllSources: function() { 281 refreshAllSources: function() {
278 let sources = this.getSources(); 282 this._log.info("refreshing all sources");
279 this._refreshSources(sources); 283 this._refreshSources(this.sources);
280 }, 284 },
281 285
282 _refreshSources: function(aSources) { 286 _refreshSources: function(sources) {
283 for each (let source in aSources) { 287 for each (let source in sources) {
288 this._log.info("refreshing source " + source.name);
289
284 source.refresh(); 290 source.refresh();
285 291
286 // We reset the last refreshed timestamp here even though the refresh 292 // We reset the last refreshed timestamp here even though the refresh
287 // is asynchronous, so we don't yet know whether it has succeeded. 293 // is asynchronous, so we don't yet know whether it has succeeded.
288 // The upside of this approach is that we don't keep trying to refresh 294 // The upside of this approach is that we don't keep trying to refresh