-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (84 loc) · 2.73 KB
/
index.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
// ****************************************************************************************************
// Init
// ****************************************************************************************************
// native dependencies
const spawn = require('child_process').spawn;
// ****************************************************************************************************
// Module Exports
// ****************************************************************************************************
module.exports = function(){
// constructor variables
let children = {};
let self = this;
// get command array for node spawn
this.getCommand = function(cmdString){
return cmdString.replace(/\[\r\n]/gm, "").replace(/\s\s+/g, ' ').trim().split(" ")
}
// get child instances
this.getInstances = function(childName){
if(!children.hasOwnProperty(childName)){
children[childName] = [];
}
return children[childName];
}
// kill child instances
this.killInstances = function(childName){
if(childName && children.hasOwnProperty(childName) && children[childName].length){
children[childName] = children[childName].filter(function(child){
child.kill()
return false;
})
return true;
} else {
return false;
}
}
// spawn a new child instance
this.startInstance = function(childName, cmd){
// console.log('starting new instance');
let cmdArr = self.getCommand(cmd)
let subProcess = spawn(cmdArr[0], cmdArr.slice(1), [], {
stdio: 'inherit'
})
subProcess.stdout.on('data', function(data){
console.log(data.toString().replace(/\n$/,""));
})
subProcess.stderr.on('data', function(data){
console.log(data.toString().replace(/\n$/,""));
})
children[childName] = Array.isArray(children[childName]) ? children[childName] : [];
children[childName].push(subProcess);
subProcess.on('exit', function(code, signal){
children[childName] = children[childName].filter(function(childProcess){
return subProcess.pid != childProcess.pid;
})
})
return subProcess;
}
// spawn a new child instance, killing old ones.
this.singleInstance = async function(childName, cmd){
return new Promise(function(resolve, reject) {
if(self.getInstances(childName).length){
self.getInstances(childName)[0].on('exit', function(code, signal){
resolve(self.startInstance(childName, cmd));
});
self.killInstances(childName);
} else {
resolve(self.startInstance(childName, cmd));
}
});
}
// kill all child instances
this.killAll = function(){
let rslt = false;
Object.keys(children).forEach(function(childName){
let status = self.killInstances(childName)
rslt = rslt || status;
})
return rslt;
}
// get all child instances
this.getAll = function(){
return children;
}
}