-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathreceive_logs_topic.js
More file actions
executable file
·47 lines (38 loc) · 1.18 KB
/
receive_logs_topic.js
File metadata and controls
executable file
·47 lines (38 loc) · 1.18 KB
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
#!/usr/bin/env node
usage();
var amqp = require('amqplib');
var all = require('when').all;
var conn = amqp.connect('amqp://localhost')
var ch = conn.then(createChannel).then(null, console.warn);
ch.then(function(ch) {
var x = ch.assertExchange('topic_logs', 'topic', {durable: false});
var q = x.then(function() {
return ch.assertQueue('', {exclusive: true});
});
var ok = q.then(function(qok) {
var queue = qok.queue;
var keys = process.argv.slice(2);
return all(keys.map(function(key) {
ch.bindQueue(queue, 'topic_logs', key);
})).then(function() { return queue; });
});
ok = ok.then(function(queue) {
return ch.consume(queue, logMessage, {noAck: true});
});
return ok.then(function() {
console.log(' [*] Waiting for logs. To exit press CTRL+C');
});
function logMessage(msg) {
console.log(" [x] %s:'%s'", msg.fields.routingKey, msg.content.toString());
}
});
function createChannel(conn) {
process.once('SIGINT', function() { conn.close(); });
return conn.createChannel();
}
function usage() {
if (process.argv.length < 3) {
console.log("Usage: receive_logs_topic.js binding_key [binding_key...]");
process.exit(1);
}
}