1.2.5 • Published 5 years ago

stubborn v1.2.5

Weekly downloads
2,340
License
ISC
Repository
github
Last release
5 years ago

Stubborn

A retry engine

Install

npm install stubborn

Example

var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000
};

var stubborn = new Stubborn(task, options, callback);

stubborn.on('attemptError', onAttemptError);

stubborn.run();

function task(callback) {
  if (Math.random() > 0.2) {
    callback('Task error');
  } else {
    callback(null, 'Task result');
  }
}

function callback(err, result) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(result);
}

function onAttemptError(err) {
  console.error(err);
}

pluggable retry algorithm

All retry algorithms need the current number of attempts as input. As output, they are expected to produce a number that will be used as a factor of the delay.

var Stubborn = require('stubborn');

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: Stubborn.exponentialBackoff()
};

var stubborn = new Stubborn(task, options, callback);

implement your own:

var options = {
  maxAttempts: 5,
  delay: 1000,
  retryAlgorithm: function(attempts) {
    // delay next execution in options.delay * attempts * 2 
    // thus in attempt #2 we'll have 1000ms * 2 * 2 = 4 seconds delay
    return attempts * 2
  }
};

out of the box algorithms:

var Stubborn = require('stubborn');

var algo1 = Stubborn.exponentialBackoff(2)       // classic http://en.wikipedia.org/wiki/Exponential_backoff
var algo2 = Stubborn.simpleExponentialBackoff(2) // same as the above only without the random element
var algo3 = Stubborn.logarithmicProgression(2)   // logarithmic progression
var algo4 = Stubborn.linear(1, 0)                // ax+b
var algo5 = Stubborm.constant(1)                 // constant / fixed progression

configure using names instead of functions

var options = {
  retryAlgorithm: 'linear',
  retryAlgorithmArgs: [ 1, 0 ]
}

Methods

  • run starts specified task, call it only once
  • cancel stops retries

Events

  • run
  • onAttemptError
  • schedule
1.2.5

5 years ago

1.2.4

5 years ago

1.2.2

5 years ago

1.2.1

6 years ago

1.2.0

10 years ago

1.1.0

10 years ago

0.3.0

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago