Permalink
Browse files

adding timeout callback feature

  • Loading branch information...
1 parent a0b2d47 commit 5f1add11b6cf8c8055b6f27c231583c6fbe554c7 @tim-kos committed Feb 14, 2012
Showing with 53 additions and 7 deletions.
  1. +5 −1 Readme.md
  2. +1 −1 lib/retry.js
  3. +36 −2 lib/retry_operation.js
  4. +11 −3 test/integration/test-retry-operation.js
View
@@ -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.
View
@@ -47,4 +47,4 @@ exports._createTimeout = function(attempt, opts) {
timeout = Math.min(timeout, opts.maxTimeout);
return timeout;
-}
+};
View
@@ -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;
}
@@ -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) {
@@ -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() {
@@ -69,4 +77,4 @@ var retry = require(common.dir.lib + '/retry');
};
fn();
-})();
+})();

0 comments on commit 5f1add1

Please sign in to comment.