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
2025-05-30 12:24:21 +03:00

22 lines
660 B
JavaScript

import cluster from 'cluster';
import { cpus } from 'os';
import { handler } from './build/handler.js';
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();
app.use(handler);
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`Worker ${process.pid} listening on ${port}`)
);
}