Limit retry delay

This commit is contained in:
Gerardo Nardelli 2019-06-12 16:14:43 -03:00
parent 9e5f185bce
commit a466fe57dc
2 changed files with 12 additions and 2 deletions

@ -1,6 +1,7 @@
require('../../env') require('../../env')
const connection = require('amqp-connection-manager').connect(process.env.QUEUE_URL) const connection = require('amqp-connection-manager').connect(process.env.QUEUE_URL)
const logger = require('./logger') const logger = require('./logger')
const { getRetrySequence } = require('../utils/utils')
connection.on('connect', () => { connection.on('connect', () => {
logger.info('Connected to amqp Broker') logger.info('Connected to amqp Broker')
@ -44,7 +45,7 @@ function connectSenderToQueue({ queueName, cb }) {
nackMsg: job => channelWrapper.nack(job, false, true), nackMsg: job => channelWrapper.nack(job, false, true),
scheduleForRetry: async (data, msgRetries = 0) => { scheduleForRetry: async (data, msgRetries = 0) => {
const retries = msgRetries + 1 const retries = msgRetries + 1
const delay = retries ** 2 * 1000 const delay = getRetrySequence(retries) * 1000
const retryQueue = `${queueName}-retry-${delay}` const retryQueue = `${queueName}-retry-${delay}`
await channel.assertQueue(retryQueue, { await channel.assertQueue(retryQueue, {
durable: true, durable: true,

@ -2,6 +2,14 @@ const BigNumber = require('bignumber.js')
const promiseRetry = require('promise-retry') const promiseRetry = require('promise-retry')
const Web3 = require('web3') const Web3 = require('web3')
const retrySequence = [1, 2, 3, 5, 8, 13, 21, 34, 55, 60]
function getRetrySequence(count) {
return count > retrySequence.length
? retrySequence[retrySequence.length - 1]
: retrySequence[count - 1]
}
async function syncForEach(array, callback) { async function syncForEach(array, callback) {
for (let index = 0; index < array.length; index++) { for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array) await callback(array[index], index, array)
@ -112,5 +120,6 @@ module.exports = {
setIntervalAndRun, setIntervalAndRun,
watchdog, watchdog,
privateKeyToAddress, privateKeyToAddress,
nonceError nonceError,
getRetrySequence
} }