clustered.js 334 B

123456789101112131415
  1. import cluster, { isPrimary, setupPrimary, fork } from 'node:cluster'
  2. import { cpus } from 'node:os'
  3. if (isPrimary) {
  4. setupPrimary({
  5. exec: 'app.js',
  6. })
  7. cluster.on('exit', (worker) => {
  8. console.log(`worker ${worker.process.pid} died`)
  9. process.exit(1)
  10. })
  11. for (let i = 0; i < cpus().length; i++) {
  12. fork()
  13. }
  14. }