Skip to content

Instantly share code, notes, and snippets.

@sunnyy02
Created September 15, 2022 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyy02/987c59a4af3ce2c2cba03f9b76fa6874 to your computer and use it in GitHub Desktop.
Save sunnyy02/987c59a4af3ce2c2cba03f9b76fa6874 to your computer and use it in GitHub Desktop.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as os from 'os';
const cluster = require('node:cluster');
const numCPUs = os.cpus().length;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
if(cluster.isMaster){
console.log(`Master server started on ${process.pid}`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Restarting`);
cluster.fork();
})
} else {
console.log(`Cluster server started on ${process.pid}`)
bootstrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment