mirror of
https://github.com/nick-fields/retry.git
synced 2026-02-10 15:15:30 +00:00
minor: added tests and helper outputs for PR #15
This commit is contained in:
59
dist/index.js
vendored
59
dist/index.js
vendored
@@ -414,12 +414,27 @@ module.exports = require("path");
|
||||
/***/ 676:
|
||||
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
|
||||
|
||||
const { getInput, error, warning, info, debug } = __webpack_require__(470);
|
||||
const { getInput, error, warning, info, debug, setOutput } = __webpack_require__(470);
|
||||
const { spawn } = __webpack_require__(129);
|
||||
const { join } = __webpack_require__(622);
|
||||
const ms = __webpack_require__(156);
|
||||
var kill = __webpack_require__(791);
|
||||
|
||||
// inputs
|
||||
const TIMEOUT_MINUTES = getInputNumber('timeout_minutes', true);
|
||||
const MAX_ATTEMPTS = getInputNumber('max_attempts', true);
|
||||
const COMMAND = getInput('command', { required: true });
|
||||
const RETRY_WAIT_SECONDS = getInputNumber('retry_wait_seconds', false);
|
||||
const POLLING_INTERVAL_SECONDS = getInputNumber('polling_interval_seconds', false);
|
||||
const RETRY_ON = getInput('retry_on') || 'any';
|
||||
|
||||
const OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts';
|
||||
const OUTPUT_EXIT_CODE_KEY = 'exit_code';
|
||||
const OUTPUT_EXIT_ERROR_KEY = 'exit_error';
|
||||
|
||||
var exit;
|
||||
var done;
|
||||
|
||||
function getInputNumber(id, required) {
|
||||
const input = getInput(id, { required });
|
||||
const num = Number.parseInt(input);
|
||||
@@ -431,20 +446,15 @@ function getInputNumber(id, required) {
|
||||
return num;
|
||||
}
|
||||
|
||||
// inputs
|
||||
const TIMEOUT_MINUTES = getInputNumber('timeout_minutes', true);
|
||||
const MAX_ATTEMPTS = getInputNumber('max_attempts', true);
|
||||
const COMMAND = getInput('command', { required: true });
|
||||
const RETRY_WAIT_SECONDS = getInputNumber('retry_wait_seconds', false);
|
||||
const POLLING_INTERVAL_SECONDS = getInputNumber('polling_interval_seconds', false);
|
||||
|
||||
async function wait(ms) {
|
||||
return new Promise((r) => setTimeout(r, ms));
|
||||
}
|
||||
|
||||
async function runCmd() {
|
||||
const end_time = Date.now() + ms.minutes(TIMEOUT_MINUTES);
|
||||
var done, exit;
|
||||
|
||||
exit = 0;
|
||||
done = false;
|
||||
|
||||
var child = spawn('node', [__webpack_require__.ab + "exec.js", COMMAND], { stdio: 'inherit' });
|
||||
|
||||
@@ -457,7 +467,7 @@ async function runCmd() {
|
||||
|
||||
do {
|
||||
await wait(ms.seconds(POLLING_INTERVAL_SECONDS));
|
||||
} while (Date.now() < end_time && !done && !exit);
|
||||
} while (Date.now() < end_time && !done);
|
||||
|
||||
if (!done) {
|
||||
kill(child.pid);
|
||||
@@ -465,7 +475,7 @@ async function runCmd() {
|
||||
throw new Error(`Timeout of ${TIMEOUT_MINUTES}m hit`);
|
||||
} else if (exit > 0) {
|
||||
await retryWait();
|
||||
throw new Error(`Child_process exited with error`);
|
||||
throw new Error(`Child_process exited with error code ${exit}`);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -481,12 +491,20 @@ async function retryWait() {
|
||||
async function runAction() {
|
||||
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||
try {
|
||||
// just keep overwriting attempts output
|
||||
setOutput(OUTPUT_TOTAL_ATTEMPTS_KEY, attempt);
|
||||
await runCmd();
|
||||
info(`Command completed after ${attempt} attempt(s).`);
|
||||
break;
|
||||
} catch (error) {
|
||||
if (attempt === MAX_ATTEMPTS) {
|
||||
throw new Error(`Final attempt failed. ${error.message}`);
|
||||
} else if (!done && RETRY_ON == 'nonzero') {
|
||||
// error: timeout
|
||||
throw error;
|
||||
} else if (exit > 0 && RETRY_ON == 'timeout') {
|
||||
// error: nonzero
|
||||
throw error;
|
||||
} else {
|
||||
warning(`Attempt ${attempt} failed. Reason:`, error.message);
|
||||
}
|
||||
@@ -494,10 +512,21 @@ async function runAction() {
|
||||
}
|
||||
}
|
||||
|
||||
runAction().catch((err) => {
|
||||
error(err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
runAction()
|
||||
.then(() => {
|
||||
setOutput(OUTPUT_EXIT_CODE_KEY, 0);
|
||||
process.exit(0); // success
|
||||
})
|
||||
.catch((err) => {
|
||||
error(err.message);
|
||||
|
||||
// these can be helpful to know if continue-on-error is true
|
||||
setOutput(OUTPUT_EXIT_ERROR_KEY, err.message);
|
||||
setOutput(OUTPUT_EXIT_CODE_KEY, exit > 0 ? exit : 1);
|
||||
|
||||
// exit with exact error code if available, otherwise just exit with 1
|
||||
process.exit(exit > 0 ? exit : 1);
|
||||
});
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
Reference in New Issue
Block a user