Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created February 5, 2017 06:59
Show Gist options
  • Save ahawkins/fbe525b3eecf4bc312cf2afc23527792 to your computer and use it in GitHub Desktop.
Save ahawkins/fbe525b3eecf4bc312cf2afc23527792 to your computer and use it in GitHub Desktop.
let redis = require('redis');
let retry = require('retry')
function ping(callback) {
let operation = retry.operation({
retries: process.env.ATTEMPTS ? parseInt(process.env.ATTEMPTS) : 60,
factor: 1,
randomize: false
});
operation.attempt(function(currentAttempt) {
try {
var client = redis.createClient({
url: process.env.REDIS_URL
});
client.ping(function(err, result) {
if(operation.retry(err)) {
return;
}
callback(err ? operation.mainError() : null, result);
});
} catch(err) {
if(operation.retry(err)) {
return;
}
callback(err || operation.mainError());
}
});
};
ping(function(err, result) {
if(err) {
console.log(err);
console.log('Connection failed');
process.exit(1);
} else {
console.log('Connection ok');
process.exit(0);
}
});
@danneu
Copy link

danneu commented Feb 5, 2017

a guess at how it might work after skimming https://github.com/tim-kos/node-retry

function ping (callback) {
  const operation = retry.operation({
    retries: process.env.ATTEMPTS ? parseInt(process.env.ATTEMPTS) : 60,
    factor: 1,
    randomize: false
  })

  const client = redis.createClient({
    url: process.env.REDIS_URL
  })

  operation.attempt(function (attemptCount) {
    client.ping(function (err, result) {
      if (operation.retry(err)) {
        if (err) {
          // if err is truthy, then then operation.retry(err) triggered a retry
          console.log('retrying due to ping error:', err, err.stack)
          return
        }
        // if falsey, then we simply hit retry limit and operation.retry(err) did not trigger a retry (we are done)
        callback(new Error('MAXIMUM_RETRIES'))
        return
      }

      // if we made it this far, then ping() was successful (we are done)
      callback(null, result)
    })
  })
}

i don't really understand the intended api usage of the retry module tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment