-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
50 lines (42 loc) · 1.44 KB
/
cluster.js
File metadata and controls
50 lines (42 loc) · 1.44 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const cluster = require('cluster');
const os = require('os');
const numCPUs = Math.min(os.cpus().length, process.env.WEB_CONCURRENCY || 2);
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Fork workers
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
// Worker management
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died (${signal || code}). Restarting...`);
cluster.fork();
});
cluster.on('online', (worker) => {
console.log(`Worker ${worker.process.pid} is online`);
});
// Graceful shutdown for primary
const shutdown = () => {
console.log('SIGTERM/SIGINT received. Shutting down gracefully...');
for (const id in cluster.workers) {
cluster.workers[id].process.kill('SIGTERM');
}
setTimeout(() => process.exit(0), 5000); // Allow time for workers to shut down
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
} else {
const app = require('./server.js')();
const port = process.env.PORT || 3000;
const server = app.listen(port, '0.0.0.0', () => {
console.log(`Worker ${process.pid} listening on port ${port}`);
});
// Graceful shutdown for workers
process.on('SIGTERM', () => {
console.log(`Worker ${process.pid} received SIGTERM. Closing server...`);
server.close(() => {
console.log(`Worker ${process.pid} server closed`);
process.exit(0);
});
});
}