forked from michaelliao/itranswarp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
37 lines (34 loc) · 1.03 KB
/
queue.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
// queue by redis:
var config = require('./config');
var
redis = require('redis'),
client = redis.createClient(config.queue.port, config.queue.host, {
parser: 'javascript'
});
module.exports = function (queueName) {
return {
// push object into queue:
push: function (value, callback) {
var s = JSON.stringify(value);
console.log('QUEUE: push: ' + s);
client.rpush(queueName, s, function (err) {
if (callback) {
callback(err);
}
});
},
// pop object into queue, return null if queue is empty:
pop: function (callback) {
client.lpop(queueName, function (err, value) {
if (err) {
return callback(err);
}
callback(null, value && JSON.parse(value));
});
},
// get size of the queue:
size: function (callback) {
client.llen(queueName, callback);
}
};
};