forked from nick-fields/retry
/
index.ts
185 lines (160 loc) · 5.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { error, warning, info, debug, setOutput } from '@actions/core';
import { execSync, spawn } from 'child_process';
import ms from 'milliseconds';
import kill from 'tree-kill';
import { getInputs, getTimeout, Inputs, validateInputs } from './inputs';
import { retryWait, wait } from './util';
const OS = process.platform;
const OUTPUT_TOTAL_ATTEMPTS_KEY = 'total_attempts';
const OUTPUT_EXIT_CODE_KEY = 'exit_code';
const OUTPUT_EXIT_ERROR_KEY = 'exit_error';
let exit: number;
let done: boolean;
function getExecutable(inputs: Inputs): string {
if (!inputs.shell) {
return OS === 'win32' ? 'powershell' : 'bash';
}
let executable: string;
switch (inputs.shell) {
case 'bash':
case 'python':
case 'pwsh': {
executable = inputs.shell;
break;
}
case 'sh': {
if (OS === 'win32') {
throw new Error(`Shell ${inputs.shell} not allowed on OS ${OS}`);
}
executable = inputs.shell;
break;
}
case 'cmd':
case 'powershell': {
if (OS !== 'win32') {
throw new Error(`Shell ${inputs.shell} not allowed on OS ${OS}`);
}
executable = inputs.shell + '.exe';
break;
}
default: {
throw new Error(
`Shell ${inputs.shell} not supported. See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsshell for supported shells`
);
}
}
return executable;
}
async function runRetryCmd(inputs: Inputs): Promise<void> {
// if no retry script, just continue
if (!inputs.on_retry_command) {
return;
}
try {
await execSync(inputs.on_retry_command, { stdio: 'inherit' });
// eslint-disable-next-line
} catch (error: any) {
info(`WARNING: Retry command threw the error ${error.message}`);
}
}
async function runCmd(attempt: number, inputs: Inputs) {
const end_time = Date.now() + getTimeout(inputs);
const executable = getExecutable(inputs);
exit = 0;
done = false;
let timeout = false;
debug(`Running command ${inputs.command} on ${OS} using shell ${executable}`);
const child =
attempt > 1 && inputs.new_command_on_retry
? spawn(inputs.new_command_on_retry, { shell: executable })
: spawn(inputs.command, { shell: executable });
child.stdout?.on('data', (data) => {
process.stdout.write(data);
});
child.stderr?.on('data', (data) => {
process.stdout.write(data);
});
child.on('exit', (code, signal) => {
debug(`Code: ${code}`);
debug(`Signal: ${signal}`);
// timeouts are killed manually
if (signal === 'SIGTERM') {
return;
}
// On Windows signal is null.
if (timeout) {
return;
}
if (code && code > 0) {
exit = code;
}
done = true;
});
do {
await wait(ms.seconds(inputs.polling_interval_seconds));
} 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`);
} else if (exit > 0) {
await retryWait(ms.seconds(inputs.retry_wait_seconds));
throw new Error(`Child_process exited with error code ${exit}`);
} else {
return;
}
}
async function runAction(inputs: Inputs) {
await validateInputs(inputs);
for (let attempt = 1; attempt <= inputs.max_attempts; attempt++) {
try {
// just keep overwriting attempts output
setOutput(OUTPUT_TOTAL_ATTEMPTS_KEY, attempt);
await runCmd(attempt, inputs);
info(`Command completed after ${attempt} attempt(s).`);
break;
// eslint-disable-next-line
} catch (error: any) {
if (attempt === inputs.max_attempts) {
throw new Error(`Final attempt failed. ${error.message}`);
} else if (!done && inputs.retry_on === 'error') {
// error: timeout
throw error;
} else if (inputs.retry_on_exit_code && inputs.retry_on_exit_code !== exit) {
throw error;
} else if (exit > 0 && inputs.retry_on === 'timeout') {
// error: error
throw error;
} else {
await runRetryCmd(inputs);
if (inputs.warning_on_retry) {
warning(`Attempt ${attempt} failed. Reason: ${error.message}`);
} else {
info(`Attempt ${attempt} failed. Reason: ${error.message}`);
}
}
}
}
}
const inputs = getInputs();
runAction(inputs)
.then(() => {
setOutput(OUTPUT_EXIT_CODE_KEY, 0);
process.exit(0); // success
})
.catch((err) => {
// exact error code if available, otherwise just 1
const exitCode = exit > 0 ? exit : 1;
if (inputs.continue_on_error) {
warning(err.message);
} else {
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, exitCode);
// if continue_on_error, exit with exact error code else exit gracefully
// mimics native continue-on-error that is not supported in composite actions
process.exit(inputs.continue_on_error ? 0 : exitCode);
});