batch-retry
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
const noop = () => {} | ||
const echo = input => input | ||
const isNotNull = n => n !== null | ||
@@ -22,3 +22,3 @@ const isError = e => e instanceof Error || (e && e.stack && e.message && typeof e.stack === 'string') | ||
onlyFinalResult: true, | ||
executor: noop | ||
executor: echo | ||
} | ||
@@ -25,0 +25,0 @@ this.options = Object.assign({}, defaultOptions, options) |
{ | ||
"name": "batch-retry", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -9,4 +9,8 @@ "main": "index.js", | ||
}, | ||
"author": "", | ||
"author": "chux0519", | ||
"license": "ISC", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/chux0519/batch-retry.git" | ||
}, | ||
"devDependencies": { | ||
@@ -13,0 +17,0 @@ "codecov": "^3.0.0", |
100
README.md
# batch-retry | ||
Helps you retry a batch of tasks that failed in bulk tasks. | ||
[](https://travis-ci.org/chux0519/batch-retry) | ||
[](https://codecov.io/gh/chux0519/batch-retry) | ||
Helps you to retry a batch of tasks. | ||
## Feature | ||
@@ -14,4 +17,99 @@ | ||
> npm install --save batch-retry | ||
> | ||
> yarn add batch-retry | ||
## Usage | ||
### Using `BatchRetry` class | ||
It provide a class called `BatchRetry` which should be contructed with a `executor`. | ||
The `executor` is a function that takes an array of size n as input, and returns another array of size n as output. | ||
A `BatchRetry` instance has a `run` method, which will pass param to `executor` and manage the retry process. | ||
When the executor itself throws, the `run` method will throws too, so `executor` must catch all tasks' error and return something for checking retry. You can simplily return the error, `BatchRetry` will check the result, if it is an error(by default, I will discuss this later), it would be recorded and wait for batch retry. | ||
```javascript | ||
const {BatchRetry} = require('batch-retry') | ||
const evenError = new Error('Even') | ||
const simpleExecutor = numbers => Promise.all(numbers | ||
.map(each => | ||
(each % 2 === 0) | ||
? Promise.reject(evenError).catch(e => e) // have to catch by hand or it will throw | ||
: Promise.resolve(each) | ||
) | ||
) | ||
// Initialize a instance. | ||
const batchRetry = new BatchRetry({ | ||
maxRetries: 5, | ||
onlyFinalResult: false, // return all the result | ||
executor: simpleExecutor | ||
}) | ||
const input = [1, 2, 3] | ||
// Run the process | ||
batchRetry.run(input).then(console.log) | ||
// will output all the result [[1],[evenError, evenError, evenError, evenError, evenError],[3]] | ||
// if onlyFinalResult set to true, it will only output [1, evenError, 3] | ||
``` | ||
### Using `buildWithBatchRetry` function | ||
It provide a helper function `buildWithBatchRetry`, to compose high order functions. | ||
```javascript | ||
const {buildWithBatchRetry} = require('batch-retry') | ||
const evenError = new Error('Even') | ||
const simpleExecutor = numbers => Promise.all(numbers | ||
.map(each => | ||
(each % 2 === 0) | ||
? Promise.reject(evenError).catch(e => e) // have to catch by hand or it will throw | ||
: Promise.resolve(each) | ||
) | ||
) | ||
// case 1: use it as a wrapper function | ||
{ | ||
const withBatchRerty = buildWithBatchRetry({ | ||
maxRetries: 5, | ||
onlyFinalResult: false // return all the result | ||
}) | ||
// wrap the executor | ||
const simpleExecutorWithBatchRetry = withBatchRerty(simpleExecutor) | ||
// Run the process | ||
simpleExecutorWithBatchRetry(input).then(console.log) | ||
// will output all the result [[1],[evenError, evenError, evenError, evenError, evenError],[3]] | ||
// if onlyFinalResult set to true, it will only output [1, evenError, 3] | ||
} | ||
// case 2: use it directly | ||
{ | ||
const simpleExecutorWithBatchRetry = buildWithBatchRetry({ | ||
maxRetries: 5, | ||
onlyFinalResult: false, // return all the result | ||
executor: simpleExecutor | ||
}) | ||
// Run the process | ||
simpleExecutorWithBatchRetry(input).then(console.log) | ||
} | ||
``` | ||
## API | ||
### `BatchRetry` | ||
constructor(options) => batchRetry: BatchRetry | ||
- `options.shouldRetry`: Function for checking retry. *Default: Check if the value is of type `error` or `error-like`(has properties `stack` and `message`) or executed times is greater than or euqal to maxRetries.* | ||
- Function: (task, result, executedTimes) = > Boolean | ||
- `options.maxRetries`: Max retry times. *Default: `5`* | ||
- Number | ||
- `options.onlyFinalResult`: Representing if it should only returns the final result. If setting to false, it will returns all results instead. For example, having final result: `[1, error, 3]` and all results: `[[1], [error, error, error, error, error], [3]]`, it means only the second task failed, and after five retries, it still failed. *Default: `true`* | ||
- Boolean | ||
- `options.executor`: Executor function. It takes an array of size `n` as input, and returns another array of the same size as output. *Required* | ||
- Function: tasks: Array => result: Array |
Sorry, the diff of this file is not supported yet
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
105364
4.01%2
-33.33%114
570.59%0
-100%