azdev-automation
Version:
Azure DevOps automation framework enables access control automation of projects, pipelines and repositories configuration in Azure DevOps Services
56 lines (55 loc) • 2.24 kB
JavaScript
;
/* eslint-disable @typescript-eslint/no-inferrable-types */
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Retryable = void 0;
const logger_1 = require("../loggers/logger");
const logger = new logger_1.Logger("release-orchestrator");
const debugLogger = logger.extend("Retry");
function Retryable(attempts = 10, timeout = 10000, empty = false) {
const debug = debugLogger.extend("retryable");
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args) {
try {
debug(`Executing <${propertyKey}> with <${attempts}> retries`);
return await retryAsync.apply(this, [originalMethod, args, attempts, timeout, empty]);
}
catch (e) {
e.message = `Failed retrying <${propertyKey}> for <${attempts}> times. ${e.message}`;
throw e;
}
};
return descriptor;
};
}
exports.Retryable = Retryable;
async function retryAsync(target, args, attempts, timeout, empty) {
const debug = debugLogger.extend("retryAsync");
try {
// @ts-ignore
let result = await target.apply(this, args);
if (!result && empty) {
if (--attempts <= 0) {
throw new Error("Empty result received");
}
debug(`Retrying <${target.name}> (empty) in <${timeout / 1000}> seconds`);
await new Promise((resolve) => setTimeout(resolve, timeout));
// @ts-ignore
result = retryAsync.apply(this, [target, args, attempts, timeout, empty]);
}
return result;
}
catch (e) {
if (--attempts <= 0) {
throw new Error(e);
}
debug(`Retrying <${target.name}> (exception) in <${timeout / 1000}> seconds`);
debug(e);
await new Promise((resolve) => setTimeout(resolve, timeout));
// @ts-ignore
return retryAsync.apply(this, [target, args, attempts, timeout]);
}
}