-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtry.js
38 lines (33 loc) · 1.03 KB
/
try.js
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
#!/usr/bin/env node
var commander = require('commander')
var exec = require('sync-exec');
var sleep = require('sleep');
var pack = require('./package.json');
var attempts,
timeout,
command,
attemptCount = 0
commander
.version(pack.version)
.description(pack.description)
.option('-a, --attempts <number>', 'Maximum number of retry attempts. DEFAULT = 4')
.option('-t, --timeout <seconds>', 'Seconds between retries. DEFAULT = 4')
.option('-c, --command <cmd>', 'Command to try')
.option('-f, --force-repeat', 'Force repeated execution even if it succeeds')
.parse(process.argv)
function main() {
setArgs()
while (attemptCount++ < attempts) {
var res = exec(command, { stdio: [0, 1, 2] })
if (res.status == 0 && !commander.forceRepeat)
break
sleep.sleep(parseInt(timeout))
}
}
function setArgs() {
timeout = commander.timeout || 4
attempts = commander.attempts || 4
command = commander.command || ''
}
main()
process.on('SIGINT', process.exit());