Create a channel that supports an exponential-backoff strategy for retrying failed attempts to consume messages on a RabbitMQ queue.
Installation (via npm)
$ npm install -S amqplib-retryable
const amqplib = require( "amqplib" );
const retryable = require( "amqplib-retryable" );
Promise
.resolve( amqplib.connect( "amqp://localhost" ) )
.then( conn => conn.createChannel() )
.then( channel => retryable( channel, {
initialDelay: 5000,
maxRetries: 5,
separator: "."
} )
.then( channel => {
return channel.consume( QUEUES.consumer, ( msg ) => {
// if this handler throws an error or returns a rejected promise, it will be retried
msg.ack();
console.log( msg );
} );
} )
.catch( err => {
console.error( "Failed to process", err );
} );
channel (required): Amqplib channel. See: connection.createChannel()
initialDelay (optional): Delay in milliseconds between retries. Default: 5000
.
maxRetries (optional): Maximum number of retries before dead-lettering. Default: 5
.
separator (options): The retry queue separator to use (ie, delayed.retry.consumer-queue.10s
). Default: .
.