mirror of
https://github.com/nick-fields/retry.git
synced 2026-02-10 07:05:29 +00:00
Compare commits
3 Commits
snyk-upgra
...
nrf/add-te
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
634988cced | ||
|
|
cc15f2ffff | ||
|
|
db9d6ef914 |
42
dist/index.js
vendored
42
dist/index.js
vendored
@@ -891,6 +891,21 @@ exports.retryWait = retryWait;
|
|||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
var __extends = (this && this.__extends) || (function () {
|
||||||
|
var extendStatics = function (d, b) {
|
||||||
|
extendStatics = Object.setPrototypeOf ||
|
||||||
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||||
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||||||
|
return extendStatics(d, b);
|
||||||
|
};
|
||||||
|
return function (d, b) {
|
||||||
|
if (typeof b !== "function" && b !== null)
|
||||||
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
||||||
|
extendStatics(d, b);
|
||||||
|
function __() { this.constructor = d; }
|
||||||
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||||
|
};
|
||||||
|
})();
|
||||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||||
return new (P || (P = Promise))(function (resolve, reject) {
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
@@ -941,8 +956,19 @@ var OS = process.platform;
|
|||||||
var OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts';
|
var OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts';
|
||||||
var OUTPUT_EXIT_CODE_KEY = 'exit_code';
|
var OUTPUT_EXIT_CODE_KEY = 'exit_code';
|
||||||
var OUTPUT_EXIT_ERROR_KEY = 'exit_error';
|
var OUTPUT_EXIT_ERROR_KEY = 'exit_error';
|
||||||
var exit;
|
|
||||||
var done;
|
var done;
|
||||||
|
var ErrorWithCode = /** @class */ (function (_super) {
|
||||||
|
__extends(ErrorWithCode, _super);
|
||||||
|
function ErrorWithCode(message, code) {
|
||||||
|
var _this = _super.call(this) || this;
|
||||||
|
_this.message = message;
|
||||||
|
_this.code = code;
|
||||||
|
_this.code = code;
|
||||||
|
_this.message = message;
|
||||||
|
return _this;
|
||||||
|
}
|
||||||
|
return ErrorWithCode;
|
||||||
|
}(Error));
|
||||||
function getExecutable(inputs) {
|
function getExecutable(inputs) {
|
||||||
if (!inputs.shell) {
|
if (!inputs.shell) {
|
||||||
return OS === 'win32' ? 'powershell' : 'bash';
|
return OS === 'win32' ? 'powershell' : 'bash';
|
||||||
@@ -1006,7 +1032,7 @@ function runRetryCmd(inputs) {
|
|||||||
function runCmd(attempt, inputs) {
|
function runCmd(attempt, inputs) {
|
||||||
var _a, _b;
|
var _a, _b;
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var end_time, executable, timeout, child;
|
var end_time, executable, exit, timeout, child;
|
||||||
return __generator(this, function (_c) {
|
return __generator(this, function (_c) {
|
||||||
switch (_c.label) {
|
switch (_c.label) {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -1056,13 +1082,13 @@ function runCmd(attempt, inputs) {
|
|||||||
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
|
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
|
||||||
case 5:
|
case 5:
|
||||||
_c.sent();
|
_c.sent();
|
||||||
throw new Error("Timeout of ".concat((0, inputs_1.getTimeout)(inputs), "ms hit"));
|
throw new ErrorWithCode("Timeout of ".concat((0, inputs_1.getTimeout)(inputs), "ms hit"), exit);
|
||||||
case 6:
|
case 6:
|
||||||
if (!(exit > 0)) return [3 /*break*/, 8];
|
if (!(exit > 0)) return [3 /*break*/, 8];
|
||||||
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
|
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
|
||||||
case 7:
|
case 7:
|
||||||
_c.sent();
|
_c.sent();
|
||||||
throw new Error("Child_process exited with error code ".concat(exit));
|
throw new ErrorWithCode("Child_process exited with error code ".concat(exit), exit);
|
||||||
case 8: return [2 /*return*/];
|
case 8: return [2 /*return*/];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -1070,7 +1096,7 @@ function runCmd(attempt, inputs) {
|
|||||||
}
|
}
|
||||||
function runAction(inputs) {
|
function runAction(inputs) {
|
||||||
return __awaiter(this, void 0, void 0, function () {
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
var attempt, error_2;
|
var attempt, error_2, exit;
|
||||||
return __generator(this, function (_a) {
|
return __generator(this, function (_a) {
|
||||||
switch (_a.label) {
|
switch (_a.label) {
|
||||||
case 0: return [4 /*yield*/, (0, inputs_1.validateInputs)(inputs)];
|
case 0: return [4 /*yield*/, (0, inputs_1.validateInputs)(inputs)];
|
||||||
@@ -1092,8 +1118,9 @@ function runAction(inputs) {
|
|||||||
return [3 /*break*/, 13];
|
return [3 /*break*/, 13];
|
||||||
case 5:
|
case 5:
|
||||||
error_2 = _a.sent();
|
error_2 = _a.sent();
|
||||||
|
exit = error_2 instanceof ErrorWithCode ? error_2.code : 1;
|
||||||
if (!(attempt === inputs.max_attempts)) return [3 /*break*/, 6];
|
if (!(attempt === inputs.max_attempts)) return [3 /*break*/, 6];
|
||||||
throw new Error("Final attempt failed. ".concat(error_2.message));
|
throw new ErrorWithCode("Final attempt failed. ".concat(error_2.message), exit);
|
||||||
case 6:
|
case 6:
|
||||||
if (!(!done && inputs.retry_on === 'error')) return [3 /*break*/, 7];
|
if (!(!done && inputs.retry_on === 'error')) return [3 /*break*/, 7];
|
||||||
// error: timeout
|
// error: timeout
|
||||||
@@ -1131,8 +1158,7 @@ runAction(inputs)
|
|||||||
process.exit(0); // success
|
process.exit(0); // success
|
||||||
})
|
})
|
||||||
.catch(function (err) {
|
.catch(function (err) {
|
||||||
// exact error code if available, otherwise just 1
|
var exitCode = err instanceof ErrorWithCode ? err.code : 1;
|
||||||
var exitCode = exit > 0 ? exit : 1;
|
|
||||||
if (inputs.continue_on_error) {
|
if (inputs.continue_on_error) {
|
||||||
(0, core_1.warning)(err.message);
|
(0, core_1.warning)(err.message);
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/index.ts
21
src/index.ts
@@ -11,9 +11,16 @@ const OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts';
|
|||||||
const OUTPUT_EXIT_CODE_KEY = 'exit_code';
|
const OUTPUT_EXIT_CODE_KEY = 'exit_code';
|
||||||
const OUTPUT_EXIT_ERROR_KEY = 'exit_error';
|
const OUTPUT_EXIT_ERROR_KEY = 'exit_error';
|
||||||
|
|
||||||
let exit: number;
|
|
||||||
let done: boolean;
|
let done: boolean;
|
||||||
|
|
||||||
|
class ErrorWithCode extends Error {
|
||||||
|
constructor(readonly message: string, readonly code: number) {
|
||||||
|
super();
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getExecutable(inputs: Inputs): string {
|
function getExecutable(inputs: Inputs): string {
|
||||||
if (!inputs.shell) {
|
if (!inputs.shell) {
|
||||||
return OS === 'win32' ? 'powershell' : 'bash';
|
return OS === 'win32' ? 'powershell' : 'bash';
|
||||||
@@ -71,7 +78,7 @@ async function runCmd(attempt: number, inputs: Inputs) {
|
|||||||
const end_time = Date.now() + getTimeout(inputs);
|
const end_time = Date.now() + getTimeout(inputs);
|
||||||
const executable = getExecutable(inputs);
|
const executable = getExecutable(inputs);
|
||||||
|
|
||||||
exit = 0;
|
let exit = 0;
|
||||||
done = false;
|
done = false;
|
||||||
let timeout = false;
|
let timeout = false;
|
||||||
|
|
||||||
@@ -117,10 +124,10 @@ async function runCmd(attempt: number, inputs: Inputs) {
|
|||||||
timeout = true;
|
timeout = true;
|
||||||
kill(child.pid);
|
kill(child.pid);
|
||||||
await retryWait(ms.seconds(inputs.retry_wait_seconds));
|
await retryWait(ms.seconds(inputs.retry_wait_seconds));
|
||||||
throw new Error(`Timeout of ${getTimeout(inputs)}ms hit`);
|
throw new ErrorWithCode(`Timeout of ${getTimeout(inputs)}ms hit`, exit);
|
||||||
} else if (exit > 0) {
|
} else if (exit > 0) {
|
||||||
await retryWait(ms.seconds(inputs.retry_wait_seconds));
|
await retryWait(ms.seconds(inputs.retry_wait_seconds));
|
||||||
throw new Error(`Child_process exited with error code ${exit}`);
|
throw new ErrorWithCode(`Child_process exited with error code ${exit}`, exit);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -138,8 +145,9 @@ async function runAction(inputs: Inputs) {
|
|||||||
break;
|
break;
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
const exit = error instanceof ErrorWithCode ? error.code : 1;
|
||||||
if (attempt === inputs.max_attempts) {
|
if (attempt === inputs.max_attempts) {
|
||||||
throw new Error(`Final attempt failed. ${error.message}`);
|
throw new ErrorWithCode(`Final attempt failed. ${error.message}`, exit);
|
||||||
} else if (!done && inputs.retry_on === 'error') {
|
} else if (!done && inputs.retry_on === 'error') {
|
||||||
// error: timeout
|
// error: timeout
|
||||||
throw error;
|
throw error;
|
||||||
@@ -168,8 +176,7 @@ runAction(inputs)
|
|||||||
process.exit(0); // success
|
process.exit(0); // success
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
// exact error code if available, otherwise just 1
|
const exitCode = err instanceof ErrorWithCode ? err.code : 1;
|
||||||
const exitCode = exit > 0 ? exit : 1;
|
|
||||||
|
|
||||||
if (inputs.continue_on_error) {
|
if (inputs.continue_on_error) {
|
||||||
warning(err.message);
|
warning(err.message);
|
||||||
|
|||||||
Reference in New Issue
Block a user