fix: Retry on timeout (#106)

* fix: do not set return value for timed out runs

Commands that are killed manually due to timeout rarely returns a
success status code (0). These codes should not be treated as errors
but simply produced because of the timeout.

* fix(windows): use variable to track timeout

Use a variable to track timeout instead of relying on SIGTERM, as
processes on Windows are not killed using signals.
This commit is contained in:
Trung Nguyen
2022-12-30 09:37:40 +07:00
committed by GitHub
parent 0711ba3d78
commit 943e742917
2 changed files with 23 additions and 7 deletions

14
dist/index.js vendored
View File

@@ -1005,7 +1005,7 @@ function runRetryCmd(inputs) {
function runCmd(attempt, inputs) {
var _a, _b;
return __awaiter(this, void 0, void 0, function () {
var end_time, executable, child;
var end_time, executable, timeout, child;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
@@ -1013,6 +1013,7 @@ function runCmd(attempt, inputs) {
executable = getExecutable(inputs);
exit = 0;
done = false;
timeout = false;
(0, core_1.debug)("Running command ".concat(inputs.command, " on ").concat(OS, " using shell ").concat(executable));
child = attempt > 1 && inputs.new_command_on_retry
? (0, child_process_1.spawn)(inputs.new_command_on_retry, { shell: executable })
@@ -1026,13 +1027,17 @@ function runCmd(attempt, inputs) {
child.on('exit', function (code, signal) {
(0, core_1.debug)("Code: ".concat(code));
(0, core_1.debug)("Signal: ".concat(signal));
if (code && code > 0) {
exit = code;
}
// timeouts are killed manually
if (signal === 'SIGTERM') {
return;
}
// On Windows signal is null.
if (timeout) {
return;
}
if (code && code > 0) {
exit = code;
}
done = true;
});
_c.label = 1;
@@ -1045,6 +1050,7 @@ function runCmd(attempt, inputs) {
_c.label = 4;
case 4:
if (!(!done && child.pid)) return [3 /*break*/, 6];
timeout = true;
(0, tree_kill_1.default)(child.pid);
return [4 /*yield*/, (0, util_1.retryWait)(milliseconds_1.default.seconds(inputs.retry_wait_seconds))];
case 5:

View File

@@ -71,6 +71,7 @@ async function runCmd(attempt: number, inputs: Inputs) {
exit = 0;
done = false;
let timeout = false;
debug(`Running command ${inputs.command} on ${OS} using shell ${executable}`);
const child =
@@ -88,13 +89,21 @@ async function runCmd(attempt: number, inputs: Inputs) {
child.on('exit', (code, signal) => {
debug(`Code: ${code}`);
debug(`Signal: ${signal}`);
if (code && code > 0) {
exit = code;
}
// timeouts are killed manually
if (signal === 'SIGTERM') {
return;
}
// On Windows signal is null.
if (timeout) {
return;
}
if (code && code > 0) {
exit = code;
}
done = true;
});
@@ -103,6 +112,7 @@ async function runCmd(attempt: number, inputs: Inputs) {
} while (Date.now() < end_time && !done);
if (!done && child.pid) {
timeout = true;
kill(child.pid);
await retryWait(ms.seconds(inputs.retry_wait_seconds));
throw new Error(`Timeout of ${getTimeout(inputs)}ms hit`);