This repository has been archived on 2025-08-19. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
coinstorge/cluster-server.js

26 lines
706 B
JavaScript
Raw Normal View History

2025-05-30 12:24:21 +03:00
import cluster from 'cluster';
import { cpus } from 'os';
2025-05-30 14:35:59 +03:00
import { Server } from './build/server/index.js';
2025-05-30 12:24:21 +03:00
import express from 'express';
const numCPUs = cpus().length;
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} spawning ${numCPUs} workers…`);
for (let i = 0; i < numCPUs; i++) cluster.fork();
cluster.on('exit', (w) => {
console.warn(`Worker ${w.process.pid} died — restarting…`);
cluster.fork();
});
} else {
const app = express();
2025-05-30 14:35:59 +03:00
const server = new Server();
app.use(server.handler);
2025-05-30 12:24:21 +03:00
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`Worker ${process.pid} listening on ${port}`)
);
2025-05-30 14:35:59 +03:00
}