2022-07-07 00:38:19 +02:00
|
|
|
//
|
|
|
|
// used by properLockFile
|
|
|
|
// Source: https://github.com/tim-kos/node-retry
|
|
|
|
//
|
|
|
|
|
2022-06-08 03:15:00 +02:00
|
|
|
var RetryOperation = require('./retry_operation');
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
exports.operation = function (options) {
|
2022-06-08 03:15:00 +02:00
|
|
|
var timeouts = exports.timeouts(options);
|
|
|
|
return new RetryOperation(timeouts, {
|
2022-07-07 00:38:19 +02:00
|
|
|
forever: options && options.forever,
|
|
|
|
unref: options && options.unref,
|
|
|
|
maxRetryTime: options && options.maxRetryTime
|
2022-06-08 03:15:00 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
exports.timeouts = function (options) {
|
2022-06-08 03:15:00 +02:00
|
|
|
if (options instanceof Array) {
|
|
|
|
return [].concat(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts = {
|
|
|
|
retries: 10,
|
|
|
|
factor: 2,
|
|
|
|
minTimeout: 1 * 1000,
|
|
|
|
maxTimeout: Infinity,
|
|
|
|
randomize: false
|
|
|
|
};
|
|
|
|
for (var key in options) {
|
|
|
|
opts[key] = options[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.minTimeout > opts.maxTimeout) {
|
|
|
|
throw new Error('minTimeout is greater than maxTimeout');
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeouts = [];
|
|
|
|
for (var i = 0; i < opts.retries; i++) {
|
|
|
|
timeouts.push(this.createTimeout(i, opts));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options && options.forever && !timeouts.length) {
|
|
|
|
timeouts.push(this.createTimeout(i, opts));
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the array numerically ascending
|
2022-07-07 00:38:19 +02:00
|
|
|
timeouts.sort(function (a, b) {
|
2022-06-08 03:15:00 +02:00
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
|
|
|
|
return timeouts;
|
|
|
|
};
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
exports.createTimeout = function (attempt, opts) {
|
2022-06-08 03:15:00 +02:00
|
|
|
var random = (opts.randomize)
|
|
|
|
? (Math.random() + 1)
|
|
|
|
: 1;
|
|
|
|
|
|
|
|
var timeout = Math.round(random * opts.minTimeout * Math.pow(opts.factor, attempt));
|
|
|
|
timeout = Math.min(timeout, opts.maxTimeout);
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
};
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
exports.wrap = function (obj, options, methods) {
|
2022-06-08 03:15:00 +02:00
|
|
|
if (options instanceof Array) {
|
|
|
|
methods = options;
|
|
|
|
options = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!methods) {
|
|
|
|
methods = [];
|
|
|
|
for (var key in obj) {
|
|
|
|
if (typeof obj[key] === 'function') {
|
|
|
|
methods.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < methods.length; i++) {
|
2022-07-07 00:38:19 +02:00
|
|
|
var method = methods[i];
|
2022-06-08 03:15:00 +02:00
|
|
|
var original = obj[method];
|
|
|
|
|
|
|
|
obj[method] = function retryWrapper(original) {
|
2022-07-07 00:38:19 +02:00
|
|
|
var op = exports.operation(options);
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2022-06-08 03:15:00 +02:00
|
|
|
var callback = args.pop();
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
args.push(function (err) {
|
2022-06-08 03:15:00 +02:00
|
|
|
if (op.retry(err)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
arguments[0] = op.mainError();
|
|
|
|
}
|
|
|
|
callback.apply(this, arguments);
|
|
|
|
});
|
|
|
|
|
2022-07-07 00:38:19 +02:00
|
|
|
op.attempt(function () {
|
2022-06-08 03:15:00 +02:00
|
|
|
original.apply(obj, args);
|
|
|
|
});
|
|
|
|
}.bind(obj, original);
|
|
|
|
obj[method].options = options;
|
|
|
|
}
|
|
|
|
};
|