Created
September 15, 2022 11:20
-
-
Save sunnyy02/987c59a4af3ce2c2cba03f9b76fa6874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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