Skip to content

Commit

Permalink
adding timeout callback feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-kos committed Feb 14, 2012
1 parent a0b2d47 commit 5f1add1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
6 changes: 5 additions & 1 deletion Readme.md
Expand Up @@ -120,11 +120,15 @@ object with that message is returned.

If no errors occured so far, the value is `null`.

#### retryOperation.attempt(fn)
#### retryOperation.attempt(fn, timeoutOps)

Defines the function `fn` that is to be retried and executes it for the first
time right away. The `fn` function can receive an optional `currentAttempt` callback that represents the number of attempts to execute `fn` so far.

Optionally defines `timeoutOps` which is an object having a property `timeout` in miliseconds and a property `cb` callback function.
Whenever your retry operation takes longer than `timeout` to execute, the timeout callback function `cb` is called.


#### retryOperation.try(fn)

This is an alias for `retryOperation.attempt(fn)`. This is deprecated.
Expand Down
2 changes: 1 addition & 1 deletion lib/retry.js
Expand Up @@ -47,4 +47,4 @@ exports._createTimeout = function(attempt, opts) {
timeout = Math.min(timeout, opts.maxTimeout);

return timeout;
}
};
38 changes: 36 additions & 2 deletions lib/retry_operation.js
Expand Up @@ -3,10 +3,17 @@ function RetryOperation(timeouts) {
this._fn = null;
this._errors = [];
this._attempts = 1;
this._operationTimeout = null;
this._operationTimeoutCb = null;
this._timeout = null;
}
module.exports = RetryOperation;

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

if (!err) {
return false;
}
Expand All @@ -19,14 +26,41 @@ RetryOperation.prototype.retry = function(err) {
}

this._attempts++;
setTimeout(this._fn.bind(this, this._attempts), timeout);

var self = this;
setTimeout(function() {
self._fn(self._attempts);

if (self._operationTimeoutCb) {
self._timeout = setTimeout(function() {
self._operationTimeoutCb(self._attempts);
}, self._operationTimeout);
}
}, timeout);

return true;
};

RetryOperation.prototype.attempt = function(fn) {
RetryOperation.prototype.attempt = function(fn, timeoutOps) {
this._fn = fn;

if (timeoutOps) {
if (timeoutOps.timeout) {
this._operationTimeout = timeoutOps.timeout;
}
if (timeoutOps.cb) {
this._operationTimeoutCb = timeoutOps.cb;
}
}

this._fn(this._attempts);

var self = this;
if (this._operationTimeoutCb) {
this._timeout = setTimeout(function() {
self._operationTimeoutCb();
}, self._operationTimeout);
}
};

RetryOperation.prototype.try = function(fn) {
Expand Down
14 changes: 11 additions & 3 deletions test/integration/test-retry-operation.js
Expand Up @@ -37,11 +37,19 @@ var retry = require(common.dir.lib + '/retry');
assert.strictEqual(operation.mainError(), error2);
})();

(function testTry() {
(function testAttempt() {
var operation = retry.operation();
var fn = new Function();
operation.attempt(fn);

var timeoutOpts = {
timeout: 1,
cb: function() {}
};
operation.attempt(fn, timeoutOpts);

assert.strictEqual(fn, operation._fn);
assert.strictEqual(timeoutOpts.timeout, operation._operationTimeout);
assert.strictEqual(timeoutOpts.cb, operation._operationTimeoutCb);
})();

(function testRetry() {
Expand Down Expand Up @@ -69,4 +77,4 @@ var retry = require(common.dir.lib + '/retry');
};

fn();
})();
})();

0 comments on commit 5f1add1

Please sign in to comment.