-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (48 loc) · 1.45 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
const {fork} = require('child_process')
const Logger = require("./Library/Loggers/MasterProcessLogger")
const SERVER = {
name: "SERVER",
path: './server'
}
const COLLECTOR = {
name: "COLLECTOR",
path: './collector'
}
const PROCESS_MAP = {}
const start = async () => {
Logger.info(`Started MasterProcess with pid ${process.ppid}`);
startChildProcess(COLLECTOR.name, COLLECTOR.path)
startChildProcess(SERVER.name, SERVER.path)
};
function startChildProcess(pName, pPath) {
const child = fork(pPath);
PROCESS_MAP[pName] = child;
Logger.info(`Process ${pName} started with PID : ${child.pid}`);
child.on("exit", (code, signal) => {
if (code !== 0) {
Logger.info(`Process ${pName} crashed with code ${code}. Restarting...`)
startChildProcess(pName, pPath);
} else {
Logger.info(`Process ${pName} exited with code ${code}.`)
}
})
child.on("error", (error) => {
Logger.info(`ERROR: Failed to start process ${pName} : ${child.pid}`);
Logger.error(error)
})
Logger.info(`setting up message relaying for ${pName}`)
child.on("message", (msg) => {
if (msg.to) {
Logger.info(`Relaying message from ${msg.from} to ${msg.to} : ${msg.code}`)
if (pName !== msg.to)
PROCESS_MAP[msg.to].send(msg);
}
})
}
(() => {
try {
start();
} catch (e) {
Logger.error(e)
}
})()