-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcluster.js
More file actions
33 lines (27 loc) · 1.02 KB
/
cluster.js
File metadata and controls
33 lines (27 loc) · 1.02 KB
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
process.env.UV_THREADPOOL_SIZE = 128; // Rapidly scale Node's internal File I/O capacity
const cluster = require("cluster");
const os = require("os");
const streamStore = require("./utils/streamStore");
const numCPUs = os.cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
const worker = cluster.fork();
streamStore.setupPrimaryHandlers(worker);
}
cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Restarting...`);
const newWorker = cluster.fork();
streamStore.setupPrimaryHandlers(newWorker);
});
// Handle graceful shutdown in Master to cleanup all FFmpeg processes
const gracefulShutdown = () => {
console.log("Master received shutdown signal. Cleaning up all streams...");
streamStore.cleanupAll();
process.exit(0);
};
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);
} else {
require("./app"); // Each worker runs the Express app
}