-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
28 lines (24 loc) · 794 Bytes
/
app.js
File metadata and controls
28 lines (24 loc) · 794 Bytes
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
const cluster = require("cluster");
if (cluster.isMaster) {
// We could create more than one worker per CPU
const nbWorkers = require("os").cpus().length;
// Fork workers.
for (let i = 0; i < nbWorkers; i++) {
cluster.fork();
}
cluster.on("online", (worker) => {
console.log(`Worker ${worker.process.pid} is online`);
});
cluster.on("exit", (worker, code, signal) => {
if (worker.exitedAfterDisconnect === true) {
console.log(`Worker ${worker.process.pid} was willingly disconnected`);
} else {
console.error(`Worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
}
console.log("Starting a new worker");
cluster.fork();
});
} else {
// Code running if we're in a worker process
require("server/server");
}