-
Notifications
You must be signed in to change notification settings - Fork 58
/
process-manager.js
45 lines (43 loc) · 1.08 KB
/
process-manager.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
const fs = require('fs');
const exec = require('child_process').exec;
class ProcessManager {
constructor() {
this.processes = [];
}
startProcess(opts, callback) {
const proc = exec(
opts.command,
opts.options
);
if (opts.waitForMessage) {
proc.stdout.on('data', function waitForMessage(data) {
if (data.toString().match(opts.waitForMessage)) {
if (callback) {
callback();
}
}
});
}
if (!opts.silent) {
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
}
if (opts.logFile) {
const logStream = fs.createWriteStream(opts.logFile, {flags: 'a'});
proc.stdout.pipe(logStream);
proc.stderr.pipe(logStream);
}
proc.on('close', function (code) {
console.log(opts.name, 'exited with code ' + code);
this.killAll();
this.process.exit(code);
});
this.processes.push(proc);
}
killAll() {
for (let i = 0; i < this.processes.length; i += 1) {
this.processes[i].kill();
}
}
}
module.exports = ProcessManager;