Created
March 14, 2015 02:18
-
-
Save dannycoates/556cac64c88e14f9f0bb to your computer and use it in GitHub Desktop.
notification log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var redis = require('redis') | |
function match(event, filter) { | |
var filterNames = Object.keys(filter) | |
for (var i = 0; i < filterNames.length; i++) { | |
var name = filterNames[i] | |
if (event[name] && event[name] === filter[name]) { | |
continue | |
} | |
else { | |
return false | |
} | |
} | |
return true | |
} | |
function RedisDB(options) { | |
this.db = redis.createClient(options.port, options.host) | |
} | |
RedisDB.create = function (options, cb) { | |
var x = new RedisDB(options) | |
x.db.once('ready', cb.bind(null, null, x)) | |
} | |
RedisDB.prototype.append = function (str, cb) { | |
this.db.rpush('fxa', str, cb) | |
} | |
RedisDB.prototype.read = function (pos, num, filter, cb) { | |
pos = +pos || 0 | |
num = num || 1000 | |
filter = filter || {} | |
this.db.lrange('fxa', pos, (pos + num), function (err, results) { | |
if (err) { return cb(err) } | |
var events = results.filter(function (event) { | |
return match(event, filter) | |
}) | |
cb( | |
null, | |
{ | |
next_pos: (pos + results.length).toString(), | |
events: events | |
} | |
) | |
}) | |
} | |
RedisDB.prototype.head = function (cb) { | |
this.db.llen('fxa', function (err, len) { | |
cb(err, len && len.toString()) | |
}) | |
} | |
RedisDB.prototype.tail = function (cb) { | |
process.nextTick(cb.bind(null, null, '0')) | |
} | |
module.exports = RedisDB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see mozilla/fxa-notification-server#19