-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (90 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var util = require('util'),
Set = require('set'),
redis = require('redis'),
winston = require('winston');
winston.loggers.add('Respirator', {
console: {
'level': 'info', 'timestamp': true, 'colorize': true
}
});
function Respirator(opts){
opts = opts || {};
this.host = opts.host || 'localhost';
this.port = opts.port || 6379;
this.db = opts.db || 0;
this.pollInterval = opts.pollInterval || 10000;
this.log = winston.loggers.get(opts.loggerName || 'Respirator');
}
Respirator.prototype.toString = function(){
return util.format("Respirator('%s', '%s', %s)", this.host,
this.port, this.db);
};
Respirator.prototype.start = function(){
this.watchedKeys = new Set();
this.log.verbose("Starting interval");
this.interval = setInterval(this.poll.bind(this), this.pollInterval);
};
Respirator.prototype.stop = function(){
delete this.watchedKeys;
this.log.verbose("Stopping...");
clearInterval(this.interval);
};
Respirator.prototype.watchKey = function(key){
if(!this.watchedKeys.contains(key)){
this.log.verbose(util.format("Watching key: %s", key));
this.watchedKeys.add(key);
}
};
Respirator.prototype.unwatchKey = function(key){
if(!this.watchedKeys.contains(key)){
this.log.verbose(util.format("Unwatching key: %s", key));
this.watchedKeys.remove(key);
}
};
// Get a new redis connection
Respirator.prototype.getRedis = function(){
var client = redis.createClient(this.port, this.host);
client.on('error', function(err){
this.log.error(util.format("Respirator Redis error: %s", err));
}.bind(this));
client.select(this.db);
return client;
};
Respirator.prototype.poll = function(){
this.log.verbose('Running poll...');
if(this.watchedKeys.empty()){
this.log.verbose("No keys to poll for right now.");
return null;
}
var client = this.getRedis();
var multi = client.multi();
var keys = this.watchedKeys.get();
keys.forEach(function(key){
multi.exists(key);
});
multi.exec(function(err, replies){
this.log.silly(util.format("Handling %d replies", replies.length));
this.log.silly(replies);
if(replies){
replies.forEach(function(reply, i){
var key = keys[i];
if(reply === 0){
this.log.silly(util.format("Key %s is expired %s",
key, reply));
client.publish(util.format("%s_expired", key),
JSON.stringify({'key': key}));
this.unwatchKey(key);
}
}.bind(this));
}
client.quit();
this.log.verbose("Poll complete.");
}.bind(this));
};
function createServer(opts){
var r = new Respirator(opts);
r.start();
return r;
}
module.exports = createServer;
module.exports.Respirator = Respirator;