-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprimary.mjs
More file actions
25 lines (20 loc) · 642 Bytes
/
primary.mjs
File metadata and controls
25 lines (20 loc) · 642 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
//* IMPORT
import cluster from "cluster";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const cpuCount = os.cpus().length;
console.log(`The total number of CPUs: ${cpuCount}`);
console.log(`Primary pid: ${process.pid}`);
cluster.setupPrimary({
exec: path.join(__dirname, "server.js"),
});
for (let i = 0; i < cpuCount; i++) {
cluster.fork();
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} has been killed`);
console.log("Starting another worker", code, signal);
cluster.fork();
});