diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2016-09-08 04:28:49 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2016-09-09 03:05:54 +0300 |
commit | 855c099482a8d93b7f0646bd7bcf8a31f81868e0 (patch) | |
tree | e0226dbb37c4cfd29e8c24013d449a83e5c240b4 | |
parent | 6d8ba2b4918e2295211130af68ee8a67099139e0 (diff) |
retry@0.10.0
Credit: @tim-kos
-rw-r--r-- | node_modules/retry/Readme.md | 18 | ||||
-rw-r--r-- | node_modules/retry/example/dns.js | 2 | ||||
-rw-r--r-- | node_modules/retry/example/stop.js | 40 | ||||
-rw-r--r-- | node_modules/retry/lib/retry.js | 4 | ||||
-rw-r--r-- | node_modules/retry/lib/retry_operation.js | 9 | ||||
-rw-r--r-- | node_modules/retry/package.json | 55 | ||||
-rw-r--r-- | node_modules/retry/test/integration/test-forever.js | 24 | ||||
-rw-r--r-- | node_modules/retry/test/integration/test-retry-operation.js | 70 | ||||
-rw-r--r-- | package.json | 2 |
9 files changed, 196 insertions, 28 deletions
diff --git a/node_modules/retry/Readme.md b/node_modules/retry/Readme.md index 26e50a3a7..eee05f7bb 100644 --- a/node_modules/retry/Readme.md +++ b/node_modules/retry/Readme.md @@ -78,7 +78,7 @@ milliseconds. If `options` is an array, a copy of that array is returned. The formula used to calculate the individual timeouts is: ``` -var Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout); +Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout) ``` Have a look at [this article][article] for a better explanation of approach. @@ -120,11 +120,11 @@ an array of method names which need to be wrapped. ``` retry.wrap(obj) -retry.wrap(obj, ['method1', 'method2']); +retry.wrap(obj, ['method1', 'method2']) -retry.wrap(obj, {retries: 3}); +retry.wrap(obj, {retries: 3}) -retry.wrap(obj, {retries: 3}, ['method1', 'method2']); +retry.wrap(obj, {retries: 3}, ['method1', 'method2']) ``` The `options` object can take any options that the usual call to `retry.operation` can take. @@ -139,7 +139,7 @@ Available options: If `forever` is true, the following changes happen: * `RetryOperation.errors()` will only output an array of one item: the last error. -* `RetryOperation` will repeatedly use the last item in the `timeouts` array. +* `RetryOperation` will repeatedly use the `timeouts` array. Once all of its timeouts have been used up, it restarts with the first timeout, then uses the second and so on. #### retryOperation.errors() @@ -181,6 +181,10 @@ has been reached. Otherwise it returns `true`, and retries the operation after the timeout for the current attempt number. +#### retryOperation.stop() + +Allows you to stop the operation being retried. Useful for aborting the operation on a fatal error etc. + #### retryOperation.attempts() Returns an int representing the number of attempts it took to call `fn` before it was successful. @@ -192,6 +196,10 @@ retry is licensed under the MIT license. # Changelog +0.10.0 Adding `stop` functionality, thanks to @maxnachlinger. + +0.9.0 Adding `unref` functionality, thanks to @satazor. + 0.8.0 Implementing retry.wrap. 0.7.0 Some bug fixes and made retry.createTimeout() public. Fixed issues [#10](https://github.com/tim-kos/node-retry/issues/10), [#12](https://github.com/tim-kos/node-retry/issues/12), and [#13](https://github.com/tim-kos/node-retry/issues/13). diff --git a/node_modules/retry/example/dns.js b/node_modules/retry/example/dns.js index d6351e9d0..446729b6f 100644 --- a/node_modules/retry/example/dns.js +++ b/node_modules/retry/example/dns.js @@ -28,4 +28,4 @@ faultTolerantResolve('nodejs.org', function(err, errors, addresses) { console.warn('addresses:'); console.log(addresses); -}); +});
\ No newline at end of file diff --git a/node_modules/retry/example/stop.js b/node_modules/retry/example/stop.js new file mode 100644 index 000000000..e1ceafeeb --- /dev/null +++ b/node_modules/retry/example/stop.js @@ -0,0 +1,40 @@ +var retry = require('../lib/retry'); + +function attemptAsyncOperation(someInput, cb) { + var opts = { + retries: 2, + factor: 2, + minTimeout: 1 * 1000, + maxTimeout: 2 * 1000, + randomize: true + }; + var operation = retry.operation(opts); + + operation.attempt(function(currentAttempt) { + failingAsyncOperation(someInput, function(err, result) { + + if (err && err.message === 'A fatal error') { + operation.stop(); + return cb(err); + } + + if (operation.retry(err)) { + return; + } + + cb(operation.mainError(), operation.errors(), result); + }); + }); +} + +attemptAsyncOperation('test input', function(err, errors, result) { + console.warn('err:'); + console.log(err); + + console.warn('result:'); + console.log(result); +}); + +function failingAsyncOperation(input, cb) { + return setImmediate(cb.bind(null, new Error('A fatal error'))); +} diff --git a/node_modules/retry/lib/retry.js b/node_modules/retry/lib/retry.js index 02ab14729..77428cfd0 100644 --- a/node_modules/retry/lib/retry.js +++ b/node_modules/retry/lib/retry.js @@ -33,6 +33,10 @@ exports.timeouts = function(options) { timeouts.push(this.createTimeout(i, opts)); } + if (options && options.forever && !timeouts.length) { + timeouts.push(this.createTimeout(i, opts)); + } + // sort the array numerically ascending timeouts.sort(function(a,b) { return a - b; diff --git a/node_modules/retry/lib/retry_operation.js b/node_modules/retry/lib/retry_operation.js index ad96efbdf..2b3db8e17 100644 --- a/node_modules/retry/lib/retry_operation.js +++ b/node_modules/retry/lib/retry_operation.js @@ -19,6 +19,15 @@ function RetryOperation(timeouts, options) { } module.exports = RetryOperation; +RetryOperation.prototype.stop = function() { + if (this._timeout) { + clearTimeout(this._timeout); + } + + this._timeouts = []; + this._cachedTimeouts = null; +}; + RetryOperation.prototype.retry = function(err) { if (this._timeout) { clearTimeout(this._timeout); diff --git a/node_modules/retry/package.json b/node_modules/retry/package.json index 9d2283a39..73a2819ed 100644 --- a/node_modules/retry/package.json +++ b/node_modules/retry/package.json @@ -1,41 +1,54 @@ { "_args": [ [ - "retry@latest", - "/Users/rebecca/code/npm" + { + "raw": "retry@0.10.0", + "scope": null, + "escapedName": "retry", + "name": "retry", + "rawSpec": "0.10.0", + "spec": "0.10.0", + "type": "version" + }, + "/Users/zkat/Documents/code/npm" ] ], - "_from": "retry@latest", - "_id": "retry@0.9.0", + "_from": "retry@0.10.0", + "_id": "retry@0.10.0", "_inCache": true, - "_installable": true, "_location": "/retry", "_nodeVersion": "4.2.1", + "_npmOperationalInternal": { + "host": "packages-12-west.internal.npmjs.com", + "tmp": "tmp/retry-0.10.0.tgz_1471682099847_0.5031970851123333" + }, "_npmUser": { - "email": "tim@debuggable.com", - "name": "tim-kos" + "name": "tim-kos", + "email": "tim@debuggable.com" }, "_npmVersion": "2.1.7", "_phantomChildren": {}, "_requested": { - "name": "retry", - "raw": "retry@latest", - "rawSpec": "latest", + "raw": "retry@0.10.0", "scope": null, - "spec": "latest", - "type": "tag" + "escapedName": "retry", + "name": "retry", + "rawSpec": "0.10.0", + "spec": "0.10.0", + "type": "version" }, "_requiredBy": [ + "#USER", "/" ], - "_resolved": "https://registry.npmjs.org/retry/-/retry-0.9.0.tgz", - "_shasum": "6f697e50a0e4ddc8c8f7fb547a9b60dead43678d", + "_resolved": "https://registry.npmjs.org/retry/-/retry-0.10.0.tgz", + "_shasum": "649e15ca408422d98318161935e7f7d652d435dd", "_shrinkwrap": null, - "_spec": "retry@latest", - "_where": "/Users/rebecca/code/npm", + "_spec": "retry@0.10.0", + "_where": "/Users/zkat/Documents/code/npm", "author": { - "email": "tim@debuggable.com", "name": "Tim Koschützki", + "email": "tim@debuggable.com", "url": "http://debuggable.com/" }, "bugs": { @@ -51,13 +64,13 @@ "lib": "./lib" }, "dist": { - "shasum": "6f697e50a0e4ddc8c8f7fb547a9b60dead43678d", - "tarball": "http://registry.npmjs.org/retry/-/retry-0.9.0.tgz" + "shasum": "649e15ca408422d98318161935e7f7d652d435dd", + "tarball": "https://registry.npmjs.org/retry/-/retry-0.10.0.tgz" }, "engines": { "node": "*" }, - "gitHead": "1b621cf499ef7647d005e3650006b93a8dbeb986", + "gitHead": "0616e6a6ebc49b5a36b619c8f7c414ced8c3813b", "homepage": "https://github.com/tim-kos/node-retry", "license": "MIT", "main": "index", @@ -75,5 +88,5 @@ "url": "git://github.com/tim-kos/node-retry.git" }, "scripts": {}, - "version": "0.9.0" + "version": "0.10.0" } diff --git a/node_modules/retry/test/integration/test-forever.js b/node_modules/retry/test/integration/test-forever.js new file mode 100644 index 000000000..b41307cb5 --- /dev/null +++ b/node_modules/retry/test/integration/test-forever.js @@ -0,0 +1,24 @@ +var common = require('../common'); +var assert = common.assert; +var retry = require(common.dir.lib + '/retry'); + +(function testForeverUsesFirstTimeout() { + var operation = retry.operation({ + retries: 0, + minTimeout: 100, + maxTimeout: 100, + forever: true + }); + + operation.attempt(function(numAttempt) { + console.log('>numAttempt', numAttempt); + var err = new Error("foo"); + if (numAttempt == 10) { + operation.stop(); + } + + if (operation.retry(err)) { + return; + } + }); +})(); diff --git a/node_modules/retry/test/integration/test-retry-operation.js b/node_modules/retry/test/integration/test-retry-operation.js index cecfa3b73..916936424 100644 --- a/node_modules/retry/test/integration/test-retry-operation.js +++ b/node_modules/retry/test/integration/test-retry-operation.js @@ -104,3 +104,73 @@ var retry = require(common.dir.lib + '/retry'); fn(); })(); + +(function testRetryForeverNoRetries() { + var error = new Error('some error'); + var delay = 50 + var operation = retry.operation({ + retries: null, + forever: true, + minTimeout: delay, + maxTimeout: delay + }); + + var attempts = 0; + var startTime = new Date().getTime(); + + var finalCallback = fake.callback('finalCallback'); + fake.expectAnytime(finalCallback); + + var fn = function() { + operation.attempt(function(currentAttempt) { + attempts++; + assert.equal(currentAttempt, attempts); + if (attempts !== 4 && operation.retry(error)) { + return; + } + + var endTime = new Date().getTime(); + var minTime = startTime + (delay * 3); + var maxTime = minTime + 20 // add a little headroom for code execution time + assert(endTime > minTime) + assert(endTime < maxTime) + assert.strictEqual(attempts, 4); + assert.strictEqual(operation.attempts(), attempts); + assert.strictEqual(operation.mainError(), error); + finalCallback(); + }); + }; + + fn(); +})(); + +(function testStop() { + var error = new Error('some error'); + var operation = retry.operation([1, 2, 3]); + var attempts = 0; + + var finalCallback = fake.callback('finalCallback'); + fake.expectAnytime(finalCallback); + + var fn = function() { + operation.attempt(function(currentAttempt) { + attempts++; + assert.equal(currentAttempt, attempts); + + if (attempts === 2) { + operation.stop(); + + assert.strictEqual(attempts, 2); + assert.strictEqual(operation.attempts(), attempts); + assert.strictEqual(operation.mainError(), error); + finalCallback(); + } + + if (operation.retry(error)) { + return; + } + }); + }; + + fn(); +})(); diff --git a/package.json b/package.json index 7ad45c834..0427b04d3 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "readable-stream": "~2.1.5", "realize-package-specifier": "~3.0.3", "request": "~2.74.0", - "retry": "~0.9.0", + "retry": "~0.10.0", "rimraf": "~2.5.4", "semver": "~5.1.0", "sha": "~2.0.1", |