Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

amqplib-retry

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

amqplib-retry - npm Package Compare versions

Comparing version 1.1.5 to 1.1.6

18

lib/amqp_handler_wrapper.js

@@ -1,14 +0,14 @@

const _=require('underscore');const Promise=require('bluebird');const config=require('./config');// attempts must be a number in milliseconds
const getDefaultDelay=attempts=>{const delay=Math.pow(2,attempts);if(delay>60*60*24){// the delay for the message is longer than 24 hours. Fail the message and never retry again.
return-1}return delay*1000};module.exports=function(channel,clientQueueName,failureQueueName,clientHandler,delayFunction,initializer){const errorHandler=msg=>{if(!initializer.isInitialized){// Delay in 1 MS to let the queues/exchange/bindings initialize
return Promise.delay(1).then(()=>errorHandler(msg))}_.defaults(msg,{properties:{}});_.defaults(msg.properties,{headers:{}});_.defaults(msg.properties.headers,{_retryCount:0});// _retryCount: 0 means this message has never been retried before.
msg.properties.headers._retryCount+=1;const expiration=(delayFunction||getDefaultDelay)(msg.properties.headers._retryCount);if(expiration<1){return channel.sendToQueue(failureQueueName,new Buffer(msg.content),msg.properties)}const properties={persistent:true,headers:{_originalProperties:msg.properties,// save the original properties.
'use strict';var _=require('underscore');var Promise=require('bluebird');var config=require('./config');// attempts must be a number in milliseconds
var getDefaultDelay=function getDefaultDelay(attempts){var delay=Math.pow(2,attempts);if(delay>60*60*24){// the delay for the message is longer than 24 hours. Fail the message and never retry again.
return-1}return delay*1000};module.exports=function(channel,clientQueueName,failureQueueName,clientHandler,delayFunction,initializer){var errorHandler=function errorHandler(msg){if(!initializer.isInitialized){// Delay in 1 MS to let the queues/exchange/bindings initialize
return Promise.delay(1).then(function(){return errorHandler(msg)})}_.defaults(msg,{properties:{}});_.defaults(msg.properties,{headers:{}});_.defaults(msg.properties.headers,{_retryCount:0});// _retryCount: 0 means this message has never been retried before.
msg.properties.headers._retryCount+=1;var expiration=(delayFunction||getDefaultDelay)(msg.properties.headers._retryCount);if(expiration<1){return channel.sendToQueue(failureQueueName,new Buffer(msg.content),msg.properties)}var properties={persistent:true,headers:{_originalProperties:msg.properties,// save the original properties.
_targetQueue:clientQueueName// save the target queue name we should publish to after the delay is over.
}};_.extend(properties,{expiration:expiration.toString()});return channel.publish('',config.delayQueueName,new Buffer(msg.content),properties)};const handlerWrapper=msg=>Promise.try(()=>clientHandler(msg)).catch(err=>{// Something went wrong. Let's handle this message.
}};_.extend(properties,{expiration:expiration.toString()});return channel.publish('',config.delayQueueName,new Buffer(msg.content),properties)};var handlerWrapper=function handlerWrapper(msg){return Promise.try(function(){return clientHandler(msg)}).catch(function(err){// Something went wrong. Let's handle this message.
// Adding the string 'error' to support papertrail error filters.
console.error('Error: AMQP retry handler caught the following error: ',err);return Promise.try(()=>errorHandler(msg)).catch(err=>{// Something went wrong while trying to process the erroneous message.
console.error('Error: AMQP retry handler caught the following error: ',err);return Promise.try(function(){return errorHandler(msg)}).catch(function(err){// Something went wrong while trying to process the erroneous message.
// Sending nack so the client can try to process it again.
channel.nack(msg);throw err})}).then(()=>// We ack it for the user. Either way if the message has been processed successfully or
channel.nack(msg);throw err})}).then(function(){return(// We ack it for the user. Either way if the message has been processed successfully or
// not, the message should be out of the original queue, therefore - acked.
channel.ack(msg));return handlerWrapper};
channel.ack(msg))})};return handlerWrapper};
//# sourceMappingURL=amqp_handler_wrapper.js.map

@@ -1,2 +0,2 @@

const EXCHANGE_NAME='amqplib-retry';const DELAYED_QUEUE_NAME='amqplib-retry.delayed';const READY_QUEUE_NAME='amqplib-retry.ready';const READY_ROUTE_KEY='ready';module.exports={exchangeName:EXCHANGE_NAME,delayQueueName:DELAYED_QUEUE_NAME,readyQueueName:READY_QUEUE_NAME,readyRouteKey:READY_ROUTE_KEY};
'use strict';var EXCHANGE_NAME='amqplib-retry';var DELAYED_QUEUE_NAME='amqplib-retry.delayed';var READY_QUEUE_NAME='amqplib-retry.ready';var READY_ROUTE_KEY='ready';module.exports={exchangeName:EXCHANGE_NAME,delayQueueName:DELAYED_QUEUE_NAME,readyQueueName:READY_QUEUE_NAME,readyRouteKey:READY_ROUTE_KEY};
//# sourceMappingURL=config.js.map

@@ -1,7 +0,7 @@

const Initializer=require('./initializer');const ReadyQueueConsumer=require('./ready_queue_consumer');const amqpHandlerWrapper=require('./amqp_handler_wrapper');module.exports=options=>{// validate options
'use strict';var Initializer=require('./initializer');var ReadyQueueConsumer=require('./ready_queue_consumer');var amqpHandlerWrapper=require('./amqp_handler_wrapper');module.exports=function(options){// validate options
if(!options.channel){throw new Error('\'channel\' not specified. See documentation.')}if(!options.consumerQueue){throw new Error('\'consumerQueue\' not specified. See documentation.')}if(!options.handler){throw new Error('\'handler\' not specified. See documentation.')}// set defaults
if(!options.failureQueue){options.failureQueue=options.consumerQueue+'.failure'}// initializing the objects
const initializer=new Initializer(options.channel,options.consumerQueue,options.failureQueue);const consumer=new ReadyQueueConsumer(options.channel);const wrapper=amqpHandlerWrapper(options.channel,options.consumerQueue,options.failureQueue,options.handler,options.delay,initializer);// initializing the queues, exchange and binding. Then starting the consumer
initializer.initialize().then(()=>consumer.start());// returning wrapper for given amqp handler function.
var initializer=new Initializer(options.channel,options.consumerQueue,options.failureQueue);var consumer=new ReadyQueueConsumer(options.channel);var wrapper=amqpHandlerWrapper(options.channel,options.consumerQueue,options.failureQueue,options.handler,options.delay,initializer);// initializing the queues, exchange and binding. Then starting the consumer
initializer.initialize().then(function(){return consumer.start()});// returning wrapper for given amqp handler function.
return wrapper};
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

var config=require('./config');var Promise=require('bluebird');class Initializer{constructor(channel,clientQueueName,failureQueueName){this.channel=channel;this.clientQueueName=clientQueueName;this.failureQueueName=failureQueueName}initialize(){const self=this;return Promise.try(()=>{return Promise.all([self.channel.assertQueue(config.delayQueueName,{durable:true,arguments:{'x-dead-letter-exchange':config.exchangeName,'x-dead-letter-routing-key':config.readyRouteKey}}),self.channel.assertQueue(config.readyQueueName,{durable:true}),self.channel.checkQueue(self.clientQueueName),self.channel.checkQueue(self.failureQueueName),self.channel.assertExchange(config.exchangeName,'direct',{durable:true})])}).then(()=>self.channel.bindQueue(config.readyQueueName,config.exchangeName,config.readyRouteKey)).then(()=>{self.isInitialized=true})}}module.exports=Initializer;
'use strict';var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var config=require('./config');var Promise=require('bluebird');var Initializer=function(){function Initializer(channel,clientQueueName,failureQueueName){_classCallCheck(this,Initializer);this.channel=channel;this.clientQueueName=clientQueueName;this.failureQueueName=failureQueueName}_createClass(Initializer,[{key:'initialize',value:function initialize(){var self=this;return Promise.try(function(){return Promise.all([self.channel.assertQueue(config.delayQueueName,{durable:true,arguments:{'x-dead-letter-exchange':config.exchangeName,'x-dead-letter-routing-key':config.readyRouteKey}}),self.channel.assertQueue(config.readyQueueName,{durable:true}),self.channel.checkQueue(self.clientQueueName),self.channel.checkQueue(self.failureQueueName),self.channel.assertExchange(config.exchangeName,'direct',{durable:true})])}).then(function(){return self.channel.bindQueue(config.readyQueueName,config.exchangeName,config.readyRouteKey)}).then(function(){self.isInitialized=true})}}]);return Initializer}();module.exports=Initializer;
//# sourceMappingURL=initializer.js.map

@@ -1,2 +0,2 @@

const config=require('./config');const Promise=require('bluebird');class ReadyQueueConsumer{constructor(channel){this.channel=channel}start(){const self=this;return self.channel.consume(config.readyQueueName,msg=>Promise.try(()=>{const targetQueueName=msg.properties.headers._targetQueue;const properties=msg.properties.headers._originalProperties;return self.channel.sendToQueue(targetQueueName,new Buffer(msg.content),properties)}).then(()=>self.channel.ack(msg)).catch(err=>{console.error('Error: while trying to process message from ready queue. err: '+err+', msg: '+JSON.stringify(msg));self.channel.nack(msg)}))}}module.exports=ReadyQueueConsumer;
'use strict';var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var config=require('./config');var Promise=require('bluebird');var ReadyQueueConsumer=function(){function ReadyQueueConsumer(channel){_classCallCheck(this,ReadyQueueConsumer);this.channel=channel}_createClass(ReadyQueueConsumer,[{key:'start',value:function start(){var self=this;return self.channel.consume(config.readyQueueName,function(msg){return Promise.try(function(){var targetQueueName=msg.properties.headers._targetQueue;var properties=msg.properties.headers._originalProperties;return self.channel.sendToQueue(targetQueueName,new Buffer(msg.content),properties)}).then(function(){return self.channel.ack(msg)}).catch(function(err){console.error('Error: while trying to process message from ready queue. err: '+err+', msg: '+JSON.stringify(msg));self.channel.nack(msg)})})}}]);return ReadyQueueConsumer}();module.exports=ReadyQueueConsumer;
//# sourceMappingURL=ready_queue_consumer.js.map
{
"name": "amqplib-retry",
"version": "1.1.5",
"version": "1.1.6",
"description": "Retry failed attempts to consume a message, with increasing delays between each attempt.",

@@ -48,3 +48,8 @@ "author": "Lanetix <engineering@lanetix.com> (https://github.com/lanetix/)",

"sinon": "^1.12.2"
},
"babel": {
"presets": [
"es2015"
]
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc