daemon/indexing/listener/listener.js
async function withRetrys(fn) {
  let tryCount = 0
  while (true) {
    try {
      return await fn() // Do our action.
    } catch (e) {
      // Roughly double wait time each failure
      let waitTime = Math.pow(100, 1 + tryCount / 6)
      // Randomly jiggle wait time by 20% either way. No thundering herd.
      waitTime = Math.floor(waitTime * (1.2 - Math.random() * 0.4))
      // Max out at two minutes
      waitTime = Math.min(waitTime, MAX_RETRY_WAIT_MS)
      console.log('ERROR', e)
      console.log(`will retry in ${waitTime / 1000} seconds`)
      tryCount += 1
      await new Promise(resolve => setTimeout(resolve, waitTime))
    }
    if (tryCount >= MAX_RETRYS) {
      console.log('Exiting. Maximum number of retrys reached.')
      // Now it's up to our environment to restart us.
      // Hopefully with a clean start, things will work better
      process.exit(1)
    }
  }
}
async function withRetrys(fn) {
  let tryCount = 0
  while (true) {
    try {
      return await fn() // Do our action.
    } catch (e) {
      // Roughly double wait time each failure
      let waitTime = Math.pow(100, 1 + tryCount / 6)
      // Randomly jiggle wait time by 20% either way. No thundering herd.
      waitTime = Math.floor(waitTime * (1.2 - Math.random() * 0.4))
      // Max out at two minutes
      waitTime = Math.min(waitTime, MAX_RETRY_WAIT_MS)
      console.log('ERROR', e)
      console.log(`will retry in ${waitTime / 1000} seconds`)
      tryCount += 1
      await new Promise(resolve => setTimeout(resolve, waitTime))
    }
    if (tryCount >= MAX_RETRYS) {
      console.log('Exiting. Maximum number of retrys reached.')
      // Now it's up to our environment to restart us.
      // Hopefully with a clean start, things will work better
      process.exit(1)
    }
  }
}
async function withRetrys ( fn ) { let tryCount = 0 while ( true ) { try { return await fn ( ) // Do our action. } catch ( e ) { // Roughly double wait time each failure let waitTime = Math . pow ( 100 , 1 + tryCount / 6 ) // Randomly jiggle wait time by 20% either way. No thundering herd. waitTime = Math . floor ( waitTime * ( 1.2 - Math . random ( ) * 0.4 ) ) // Max out at two minutes waitTime = Math . min ( waitTime , MAX_RETRY_WAIT_MS ) console . log ( 'ERROR' , e ) console . log ( ` ${ waitTime / 1000 } ` ) tryCount += 1 await new Promise ( resolve => setTimeout ( resolve , waitTime ) ) } if ( tryCount >= MAX_RETRYS ) { console . log ( 'Exiting. Maximum number of retrys reached.' ) // Now it's up to our environment to restart us. // Hopefully with a clean start, things will work better process . exit ( 1 ) } } }
Retrys up to 10 times, with exponential backoff, then exits the process
Retrys up to 10 times with exponential backoff then exits the process