add option retry_on

This commit is contained in:
milahu
2020-09-21 20:47:01 +02:00
committed by GitHub
parent 7841cadab1
commit d2b20569e3

View File

@@ -21,14 +21,20 @@ const MAX_ATTEMPTS = getInputNumber('max_attempts', true);
const COMMAND = getInput('command', { required: true }); const COMMAND = getInput('command', { required: true });
const RETRY_WAIT_SECONDS = getInputNumber('retry_wait_seconds', false); const RETRY_WAIT_SECONDS = getInputNumber('retry_wait_seconds', false);
const POLLING_INTERVAL_SECONDS = getInputNumber('polling_interval_seconds', false); const POLLING_INTERVAL_SECONDS = getInputNumber('polling_interval_seconds', false);
const RETRY_ON = getInput('retry_on') || 'both';
async function wait(ms) { async function wait(ms) {
return new Promise((r) => setTimeout(r, ms)); return new Promise((r) => setTimeout(r, ms));
} }
var exit;
var done;
async function runCmd() { async function runCmd() {
const end_time = Date.now() + ms.minutes(TIMEOUT_MINUTES); const end_time = Date.now() + ms.minutes(TIMEOUT_MINUTES);
var done, exit;
exit = 0;
done = false;
var child = spawn('node', [join(__dirname, 'exec.js'), COMMAND], { stdio: 'inherit' }); var child = spawn('node', [join(__dirname, 'exec.js'), COMMAND], { stdio: 'inherit' });
@@ -41,7 +47,7 @@ async function runCmd() {
do { do {
await wait(ms.seconds(POLLING_INTERVAL_SECONDS)); await wait(ms.seconds(POLLING_INTERVAL_SECONDS));
} while (Date.now() < end_time && !done && !exit); } while (Date.now() < end_time && !done);
if (!done) { if (!done) {
kill(child.pid); kill(child.pid);
@@ -49,7 +55,7 @@ async function runCmd() {
throw new Error(`Timeout of ${TIMEOUT_MINUTES}m hit`); throw new Error(`Timeout of ${TIMEOUT_MINUTES}m hit`);
} else if (exit > 0) { } else if (exit > 0) {
await retryWait(); await retryWait();
throw new Error(`Child_process exited with error`); throw new Error(`Child_process exited with error code ${exit}`);
} else { } else {
return; return;
} }
@@ -71,6 +77,12 @@ async function runAction() {
} catch (error) { } catch (error) {
if (attempt === MAX_ATTEMPTS) { if (attempt === MAX_ATTEMPTS) {
throw new Error(`Final attempt failed. ${error.message}`); 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 { } else {
warning(`Attempt ${attempt} failed. Reason:`, error.message); warning(`Attempt ${attempt} failed. Reason:`, error.message);
} }
@@ -78,7 +90,17 @@ async function runAction() {
} }
} }
runAction().catch((err) => { runAction()
.then(() => {
process.exit(0); // success
})
.catch((err) => {
error(err.message); error(err.message);
process.exit(1); if (exit > 0) {
// error: nonzero
process.exit(exit); // copy exit code
} else {
// error: Final attempt failed or timeout
process.exit(1);
}
}); });