Skip to content

Instantly share code, notes, and snippets.

@konstantinzolotarev
Created August 11, 2016 10:43
Show Gist options
  • Save konstantinzolotarev/4985615a7581c42c700c15694e0f1580 to your computer and use it in GitHub Desktop.
Save konstantinzolotarev/4985615a7581c42c700c15694e0f1580 to your computer and use it in GitHub Desktop.
Delayed job with RabbitMQ and amqplib
'use strict'
const amq = require('amqplib')
const open = amq.connect('amqp://localhost')
const q = 'tasks'
open
.then(function(conn) {
return conn.createChannel();
})
.then(function(ch) {
ch.assertExchange('tmp_exchange', 'fanout', { durable: false })
return ch.assertQueue('tmp_queue', {
deadLetterExchange: 'final_exchange',
messageTtl: 5000
})
.then(function(q) {
ch.bindQueue(q.queue, 'tmp_exchange', '')
console.log('sending message')
return ch.sendToQueue(q.queue, new Buffer('something to do'));
});
})
.catch(console.warn);
// open
// .then(function(conn) {
// return conn.createChannel();
// })
// .then(function(ch) {
// return ch.assertQueue(q).then(function(ok) {
// return ch.consume(q, function(msg) {
// if (msg !== null) {
// console.log(msg.content.toString());
// ch.ack(msg);
// }
// });
// });
// }).catch(console.warn);
open
.then((conn) => conn.createChannel())
.then((ch) => {
ch.assertExchange('final_exchange', 'fanout', { durable: false })
return ch.assertQueue('final_queue')
.then((q) => {
ch.bindQueue(q.queue, 'final_exchange', '')
ch.consume(q.queue, (msg) => {
console.log('==============================')
console.log(msg.content.toString())
console.log('==============================')
ch.ack(msg)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment