Skip to content

Instantly share code, notes, and snippets.

@poberherr
Created July 8, 2016 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poberherr/0ba25fa180ad6d33732bd0d019002163 to your computer and use it in GitHub Desktop.
Save poberherr/0ba25fa180ad6d33732bd0d019002163 to your computer and use it in GitHub Desktop.
Listens to AMQP events, for each event key creates a file and put the content in it.
// Start with node <filename>.js '#'
// Enjoy!
'use strict';
// #!/usr/bin/env node
var storage = require('node-persist');
var amqp = require('amqplib/callback_api');
var args = process.argv.slice(2);
storage.initSync();
if (args.length === 0) {
console.log('Usage: receive_logs_topic.js <facility>.<severity>');
process.exit(1);
}
amqp.connect('amqp://localhost', function (err, conn) {
conn.createChannel(function (err, ch) {
var ex = 'events';
ch.assertExchange(ex, 'topic', {durable: true});
ch.assertQueue('', {exclusive: true}, function (err, q) {
console.log(' [*] Waiting for logs. To exit press CTRL+C');
args.forEach(function (key) {
ch.bindQueue(q.queue, ex, key);
});
ch.consume(q.queue, function (msg) {
var content = {
message: msg.content.toString()
};
storage.setItem(msg.fields.routingKey, content);
console.log(' [x] %s:\'%s\'', msg.fields.routingKey, msg.content.toString());
}, {noAck: true});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment