-
Notifications
You must be signed in to change notification settings - Fork 700
Expand file tree
/
Copy pathretry.js
More file actions
51 lines (41 loc) · 1020 Bytes
/
retry.js
File metadata and controls
51 lines (41 loc) · 1020 Bytes
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
const { spawn } = require('child_process');
const [, , cmd, ...args] = process.argv;
if (!cmd) {
process.exit(-1);
}
const once = (fn) => {
let runOnce = false;
return (...args) => {
if (runOnce) {
return;
}
runOnce = true;
fn(...args);
}
};
const retry = (numRetries = 3) => {
const child = spawn(cmd, args, {
shell: process.platform === 'win32',
stdio: [0, 'pipe', 'pipe']
});
child.setMaxListeners(0);
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
const cleanupAndExit = once((error, status) => {
child.kill();
if (numRetries > 0 && (error || status !== 0)) {
retry(numRetries - 1);
} else if (error) {
console.log(error);
process.exit(-1);
} else {
process.exit(status);
}
});
const onClose = status => cleanupAndExit(null, status);
child.on('close', onClose);
child.on('error', cleanupAndExit);
};
retry();