Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operation.reset() gives ability force retry #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,29 @@ Allows you to stop the operation being retried. Useful for aborting the operatio

#### retryOperation.reset()

Resets the internal state of the operation object, so that you can call `attempt()` again as if this was a new operation object.
Resets the internal state of the operation object. You can then call `retry()` (passing an error not required), and the entire operation will begin again as if it's the first attempt.

```
var operation = retry.operation();
operation.attempt(function(currentAttempt) {
var err;
try{
doSomething();
}
catch(e){
e = err;
}

if (needToStartFromScratch()){
operation.reset();
operation.retry();
return;
}

if (operation.retry(err)) return;
});
```


#### retryOperation.attempts()

Expand Down
51 changes: 29 additions & 22 deletions lib/retry_operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function RetryOperation(timeouts, options) {
this._operationTimeoutCb = null;
this._timeout = null;
this._operationStart = null;
this._reset = false;

if (this._options.forever) {
this._cachedTimeouts = this._timeouts.slice(0);
Expand All @@ -23,8 +24,9 @@ function RetryOperation(timeouts, options) {
module.exports = RetryOperation;

RetryOperation.prototype.reset = function() {
this._attempts = 1;
this._timeouts = this._originalTimeouts;
this._attempts = 0;
this._timeouts = JSON.parse(JSON.stringify(this._originalTimeouts));
this._reset = true;
}

RetryOperation.prototype.stop = function() {
Expand All @@ -36,32 +38,37 @@ RetryOperation.prototype.stop = function() {
this._cachedTimeouts = null;
};

RetryOperation.prototype.retry = function(err) {
RetryOperation.prototype.retry = function(err, force) {
if (this._timeout) {
clearTimeout(this._timeout);
}

if (!err) {
return false;
}
var currentTime = new Date().getTime();
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
this._errors.unshift(new Error('RetryOperation timeout occurred'));
return false;

if (this._reset){
this._reset = false;
}

this._errors.push(err);

var timeout = this._timeouts.shift();
if (timeout === undefined) {
if (this._cachedTimeouts) {
// retry forever, only keep last error
this._errors.splice(this._errors.length - 1, this._errors.length);
this._timeouts = this._cachedTimeouts.slice(0);
timeout = this._timeouts.shift();
} else {
else{
if (!err) {
return false;
}
var currentTime = new Date().getTime();
if (err && currentTime - this._operationStart >= this._maxRetryTime) {
this._errors.unshift(new Error('RetryOperation timeout occurred'));
return false;
}

this._errors.push(err);

var timeout = this._timeouts.shift();
if (timeout === undefined) {
if (this._cachedTimeouts) {
// retry forever, only keep last error
this._errors.splice(this._errors.length - 1, this._errors.length);
this._timeouts = this._cachedTimeouts.slice(0);
timeout = this._timeouts.shift();
} else {
return false;
}
}
}

var self = this;
Expand Down
50 changes: 25 additions & 25 deletions test/integration/test-retry-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ var retry = require(common.dir.lib + '/retry');
var error = new Error('some error');
var operation = retry.operation([1, 2, 3]);
var attempts = 0;
var allAttempts = 0;

var finalCallback = fake.callback('finalCallback');
fake.expectAnytime(finalCallback);

var expectedFinishes = 1;
var finishes = 0;

var fn = function() {
operation.attempt(function(currentAttempt) {
attempts++;
assert.equal(currentAttempt, attempts);
if (operation.retry(error)) {
return;
}

finishes++
assert.equal(expectedFinishes, finishes);
assert.strictEqual(attempts, 4);
assert.strictEqual(operation.attempts(), attempts);
assert.strictEqual(operation.mainError(), error);

if (finishes < 2) {
attempts = 0;
expectedFinishes++;
operation.reset();
fn()
} else {
finalCallback();
}
});
};
operation.attempt(function(currentAttempt) {
attempts++;
allAttempts++;
assert.equal(currentAttempt, attempts);

// perform a single reset after 2 attempts
if (attempts === 2 && allAttempts === 2) {
attempts = 0;
operation.reset();
operation.retry();
return;
}

if (operation.retry(error)) {
return;
}

finishes++
assert.equal(expectedFinishes, finishes);
assert.strictEqual(allAttempts, 6);
assert.strictEqual(operation.attempts(), attempts);
assert.strictEqual(operation.mainError(), error);
finalCallback();
});

fn();
})();

(function testErrors() {
Expand Down