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