Skip to content

Instantly share code, notes, and snippets.

@phil-kahrl
Created August 16, 2018 19:47
Show Gist options
  • Save phil-kahrl/c157452f6b8f6b1554c76ce71e5289c3 to your computer and use it in GitHub Desktop.
Save phil-kahrl/c157452f6b8f6b1554c76ce71e5289c3 to your computer and use it in GitHub Desktop.
Function to recursively retry results for an AWS Route53 batch change request
const AWS = require('aws-sdk');
const route53 = new AWS.Route53({
apiVersion: '2013-04-01'
})
const MAX_RETRIES = 4
const RETRY_INTERVAL = 1000
const getChange = (prevAttemptCount, origResolve, origReject) => {
// Initialize the attempt if it has not been passed in
let attempt = prevAttemptCount ? prevAttemptCount : 0
return new Promise( (resolve, reject) => {
// The resolve and reject function references will be passed down the call stack.
resolve = (origResolve) ? origResolve : resolve
reject = (origReject) ? origReject : reject
try {
attempt++
console.log(`Attempt: ${attempt}`)
if (attempt > MAX_RETRIES) {
console.log('Max retries exceeded')
reject()
} else {
route53.getChange({Id: 'notarealid'}, function(err, data) {
if(err){
console.log(err)
//try again on error
setTimeout( () => {getChange(attempt, origResolve ? origResolve : resolve, origReject ? origReject : reject)}, RETRY_INTERVAL)
} else {
if(data.ChangeInfo.Status === 'INSYNC') {
resolve(data)
} else {
// Still not in sync so try again.
setTimeout( () => {getChange(attempt, origResolve ? origResolve : resolve, origReject ? origReject : reject)}, RETRY_INTERVAL )
}
}
})
}
} catch(error) {
console.log('error caught, retrying')
setTimeout( () => { getChange(attempt, origResolve ? origResolve : resolve, origReject ? origReject : reject) }, RETRY_INTERVAL )
}
})
}
module.exports = getChange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment