-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcumin-monitor.js
71 lines (55 loc) · 1.6 KB
/
cumin-monitor.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
var express = require("express"),
async = require("async"),
redis = require("redis").createClient(),
app = express(),
server = require("http").createServer(app);
app.use(express.static(__dirname + '/public'));
server.listen(1337);
var connectedClients = [];
app.get("/", function(req, res) {
res.sendfile(__dirname + "/public/index.html");
});
app.get("/stream", function(req, res, next) {
connectedClients.push(res);
res.writeHead(200, {
"content-type": "text/event-stream"
})
getQueues(function(err, queues) {
if(err) return next(err);
res.write("event: queues\ndata: " + JSON.stringify(queues) + "\n\n");
});
req.on("close", function() {
connectedClients.splice(connectedClients.indexOf(res), 1);
});
});
function getQueues(done) {
getQueueNames(function(err, queues) {
if(err) return done(err);
async.map(queues, getQueueDetails, done);
});
}
function getQueueNames(done) {
redis.smembers("cuminqueues", done);
}
function getQueueDetails(queueName, done) {
async.parallel([
function(done) { redis.hgetall("cuminmeta." + queueName, done); },
function(done) { redis.llen("cumin." + queueName, done); }
], function(err, data) {
if(err) return done(err);
var queueInfo = data[0];
queueInfo.name = queueName;
queueInfo.count = data[1];
queueInfo.now = Date.now();
done(null, queueInfo);
});
}
setInterval(function() {
if(!connectedClients.length) return;
getQueues(function(err, queues) {
var queuesStringified = JSON.stringify(queues);
connectedClients.forEach(function(client) {
client.write("event: update\ndata: " + queuesStringified + "\n\n");
});
});
}, 1000);