Created
July 8, 2016 09:08
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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