@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
43 lines • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.repeatWithBackoff = exports.ExponentialBackoff = exports.sleep = void 0;
const tslib_1 = require("tslib");
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
exports.sleep = sleep;
class ExponentialBackoff {
constructor(startMs = 10, times = 10) {
this.startMs = startMs;
this.times = times;
}
backoff() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (this.times <= 0) {
throw new Error('Aborting exponential backoff');
}
yield sleep(this.startMs);
this.startMs *= 2;
this.times++;
});
}
}
exports.ExponentialBackoff = ExponentialBackoff;
function repeatWithBackoff(func, backoff = new ExponentialBackoff(), logger = console.log) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
for (;;) {
try {
return yield func();
}
catch (e) {
const stack = e.stack
? `\nat:\n${String(e.stack)}`
: ' (no stack available)';
logger(`Retrying after exception at ${new Date().toISOString()}: ${String(e)}` + stack);
yield backoff.backoff();
}
}
});
}
exports.repeatWithBackoff = repeatWithBackoff;
//# sourceMappingURL=backoff.js.map