-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (46 loc) · 1.34 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
const cluster = require('cluster');
var replWorker = require('./lib/repl-worker.js');
var worker;
launch(process.argv[2] || null, process.argv[3] || null);
function launch(appPath, configJSONPath) {
if (!appPath) {
throw new Error(`Application path is invalid. Provided path: ${appPath}`);
return;
}
if (cluster.isMaster) {
forkNewWorker(appPath, configJSONPath);
} else {
replWorker.launch();
}
}
function forkNewWorker(appPath, configJSONPath) {
if (worker) {
worker.process.removeAllListeners('message'); // remove worker process listeners to prevent "Error: IPC channel is already disconnected"
// Waits for the exit event before forking a new worker
worker.once('exit', (code, signal) => {
_forkNewWorker(appPath, configJSONPath);
});
try{
worker.disconnect();
worker.kill();
} catch(err){ console.log(err); }
return;
}
_forkNewWorker(appPath, configJSONPath);
}
function _forkNewWorker(appPath, configJSONPath) {
worker = cluster.fork({appPath:appPath, configJSONPath:configJSONPath});
worker.process.on('message', (msg) => {
if (msg.type==="CMD" && msg.value==="QUIT"){
process.exit();
return;
}
if (msg.type==="CMD" && msg.value==="RESTART"){
forkNewWorker(appPath, configJSONPath);
return;
}
});
}
module.exports = {
launch
}