-
Notifications
You must be signed in to change notification settings - Fork 3
/
processWorker.js
103 lines (90 loc) · 2.34 KB
/
processWorker.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
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
const _ = require('lodash')
const {
killProcess,
attachKillListener,
getProcessById,
createProcess,
} = require('./utils/processes')
const { updateCommand } = require('./utils/commands')
const colors = require('colors')
const kill = require('tree-kill')
const { getConfig } = require('./config')
colors.enabled = true
function stop(command) {
// if (_.isString(command)) {
// command = getCommandById(command)
// }
killProcess(command.id)
updateCommand(command, { isStopping: true })
}
function run(command) {
// if (_.isString(command)) {
// command = getCommandById(command)
// }
const config = getConfig(command.configId)
const taskId = command.id
const isWeb = getConfig(command.configId).web
const proc = createProcess(command, config)
if (isWeb) {
updateCommand(command, { isRun: true })
proc.stdout.on('data', rawLog =>
updateCommand(command, {
log: rawLog.toString(),
isRun: true,
})
)
proc.stderr.on('data', rawLog =>
updateCommand(command, {
log: rawLog.toString(),
isRun: true,
})
)
proc.on('close', code => {
killProcess(taskId)
updateCommand(command, {
isRun: false,
log: !code ? 'Exited Successfully' : 'Exited with exit code ' + code,
})
})
proc.on('error', () => {
killProcess(taskId)
updateCommand(command, {
isRun: false,
log: 'Failed to execute command',
})
})
attachKillListener(taskId)
} else {
const defaultOutputHandler = log => {
process.stdout.write(log.toString())
}
proc.stdout.on('data', defaultOutputHandler)
proc.stdout.on('error', defaultOutputHandler)
proc.stderr.on('data', defaultOutputHandler)
}
}
function reRun(command) {
const isLaunching = true
const proc = getProcessById(command.id)
if (proc) {
kill(proc.pid, 'SIGINT', () => {
updateCommand(command, { isLaunching })
setTimeout(() => run(command), 1000)
})
updateCommand(command, { isLaunching })
} else {
updateCommand(command, { isLaunching })
run(command)
}
}
function runAll(commands) {
_.each(commands, run)
}
function stopAll(commands) {
_.each(commands, stop)
}
module.exports.runAll = runAll
module.exports.stopAll = stopAll
module.exports.run = run
module.exports.stop = stop
module.exports.reRun = reRun