@pixellot/pxlt-rabbit-handler
Version:
A generic class that handles RabbitMQ connection, consume and produce functionality.
72 lines (67 loc) • 3.52 kB
JavaScript
const setupSubscriptionRetry = (channel) => {
const config = {
exchanges: {
subscriptionsExchange: {
name: 'subscriptions-exchange',
type: 'topic'
}
},
queues: {
subscriptionsHandler: {
name: 'subscriptions',
routingKey: '#'
}
}
};
return channel.assertQueue(config.queues.subscriptionsHandler.name, { durable: true })
.then(() => channel.assertExchange(config.exchanges.subscriptionsExchange.name, config.exchanges.subscriptionsExchange.type, { durable: true }))
.then(() => channel.bindQueue(config.queues.subscriptionsHandler.name, config.exchanges.subscriptionsExchange.name, config.queues.subscriptionsHandler.routingKey))
.then(() => channel.assertExchange(`${config.exchanges.subscriptionsExchange.name}.retry`, config.exchanges.subscriptionsExchange.type, { durable: true }))
.then(() => channel.assertQueue(`${config.queues.subscriptionsHandler.name}.retry.1.seconds`, {
durable: true,
messageTtl: 1000,
deadLetterExchange: config.exchanges.subscriptionsExchange.name
}))
.then(() => channel.bindQueue(`${config.queues.subscriptionsHandler.name}.retry.1.seconds`, `${config.exchanges.subscriptionsExchange.name}.retry`, '*.1'))
.then(() => channel.assertQueue(`${config.queues.subscriptionsHandler.name}.retry.2.seconds`, {
durable: true,
messageTtl: 2000,
deadLetterExchange: config.exchanges.subscriptionsExchange.name
}))
.then(() => channel.bindQueue(`${config.queues.subscriptionsHandler.name}.retry.2.seconds`, `${config.exchanges.subscriptionsExchange.name}.retry`, '*.2'))
.then(() => channel.assertQueue(`${config.queues.subscriptionsHandler.name}.retry.3.seconds`, {
durable: true,
messageTtl: 3000,
deadLetterExchange: config.exchanges.subscriptionsExchange.name
}))
.then(() => channel.bindQueue(`${config.queues.subscriptionsHandler.name}.retry.3.seconds`, `${config.exchanges.subscriptionsExchange.name}.retry`, '*.3'))
.then(() => channel.assertQueue(`${config.queues.subscriptionsHandler.name}.retry.4.seconds`, {
durable: true,
messageTtl: 4000,
deadLetterExchange: config.exchanges.subscriptionsExchange.name
}))
.then(() => channel.bindQueue(`${config.queues.subscriptionsHandler.name}.retry.4.seconds`, `${config.exchanges.subscriptionsExchange.name}.retry`, '*.4'));
};
const setupSimpleExcahngeAndQueue = (channel) => {
const config = {
exchanges: {
subscriptionsExchange: {
name: 'subscriptions-exchange',
type: 'topic'
}
},
queues: {
subscriptionsHandler: {
name: 'subscriptions',
routingKey: '#'
}
}
};
return channel.assertQueue(config.queues.subscriptionsHandler.name, { durable: true })
.then(() => channel.assertExchange(config.exchanges.subscriptionsExchange.name, config.exchanges.subscriptionsExchange.type, { durable: true }))
.then(() => channel.bindQueue(config.queues.subscriptionsHandler.name, config.exchanges.subscriptionsExchange.name, config.queues.subscriptionsHandler.routingKey));
};
module.exports = {
setupSubscriptionRetry,
setupSimpleExcahngeAndQueue
};