-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathretry.js
More file actions
37 lines (32 loc) · 1.08 KB
/
Copy pathretry.js
File metadata and controls
37 lines (32 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const Promise = require('bluebird');
const retry_ = require('bluebird-retry');
const _ = require('lodash');
const debug = require('debug')('retry');
let defaults = {
retry_options: {},
func_options: {
timeout: 1000,
backoff: 1
}
}
function retry(func, retry_options_, func_options_) {
let retry_options = _.assign(defaults.retry_options, retry_options_)
let func_options = _.assign(defaults.func_options, func_options_);
let timeout = func_options.timeout;
let count = 1;
return retry_(function() {
if (timeout > 0) {
debug(func.name, 'times:', count++, 'timeout:', timeout);
let timeout_ = timeout;
timeout *= func_options.backoff;
return func().timeout(timeout_);
} else {
return func();
}
}, retry_options);
}
retry.setDefaults = function(retry_options = {}, func_options = {}) {
defaults.retry_options = _.assign(defaults.retry_options, retry_options);
defaults.func_options = _.assign(defaults.func_options, func_options);
};
module.exports = retry;