forked from dsckiet/website-backend-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (30 loc) · 826 Bytes
/
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
const cluster = require("cluster");
const os = require("os");
require("newrelic");
const runExpressServer = require("./app");
let { NODE_ENV } = require("./config/index");
// Check if current process is master.
if (cluster.isMaster) {
// Get total CPU cores.
let cpuCount;
if (NODE_ENV === "production") {
cpuCount = os.cpus().length;
} else {
cpuCount = 1;
}
console.log(cpuCount);
// Spawn a worker for every core.
for (let j = 0; j < cpuCount; j++) {
cluster.fork();
}
} else {
// This is not the master process, so we spawn the express server.
runExpressServer();
}
// Cluster API has a variety of events.
// Here we are creating a new process if a worker die.
cluster.on("exit", function (worker) {
console.log(`Worker ${worker.id} died'`);
console.log(`Staring a new one...`);
cluster.fork();
});