-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
69 lines (55 loc) · 1.64 KB
/
run.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
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
const execa = require('execa')
const running = {}
const {readFile, writeFile, stat, mkdir, createWriteStream} = require('fs').promises
setInterval(() => {
// console.log({running: Object.keys(running)})
for (const name in running) {
if (running[name].exitCode != null) {
delete running[name]
}
}
}, 100)
const run = async (command, name, opts = {}) => {
console.log({run: command, name, opts})
let r = running[name]
if (opts.restart && r) {
r.kill()
delete running[name]
r = null
}
if (r) {
console.log('already runnning ' + name)
return Promise.resolve(r)
}
const [shell, ...args] = String(opts.shell || 'bash -c').split(/\s+/)
opts.shell = false
delete opts.input
console.log(shell, opts)
// r = execa('"""'+command+'"""', [], opts)
console.log(shell, args)
r = execa(shell, concat(args, [command]), opts)
if (name) running[name] = r
r.stdout.pipe(process.stdout)
r.stderr.pipe(process.stderr)
console.log({running: keys(running)})
return r
.then(tap(console.log))
// .catch(console.log)
}
const kill = names => names.map(x => running[x] && running[x].kill())
const healthcheck = (c, name, retry, opts) => {
const r = running[name]
if (r)
return Promise.resolve()
if (!c.healthcheck)
return Promise.reject({})
return run(c.healthcheck, `healthcheck ${name}`, opts)
.catch(e => {
if (retry > 0) return wait(100)
.then(() => healthcheck(c, name, retry-1, opts))
throw e
})
}
const user2uid = user => execa(`id -u ${user}`)
const group2gid = group => execa(`id -g ${group}`)
module.exports = {run, healthcheck, user2uid, group2gid}