50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import cluster from 'cluster'
|
|
import os from 'os'
|
|
|
|
export class Cluster {
|
|
static register(workers: Number, callback: Function): void {
|
|
if (cluster.isPrimary) {
|
|
const cpus = os.cpus().length
|
|
|
|
consola.info(`Primary server started on ${process.pid}`)
|
|
consola.info(`CPU:${cpus}`)
|
|
// ensure workers exit cleanly
|
|
process.on('SIGINT', () => {
|
|
consola.info('Cluster shutting down...')
|
|
for (const id in cluster.workers) {
|
|
cluster.workers[id]?.kill()
|
|
}
|
|
// exit the master process
|
|
process.exit(0)
|
|
})
|
|
|
|
if (workers > cpus) workers = cpus
|
|
|
|
for (let i = 0; i < workers; i++) {
|
|
cluster.fork()
|
|
}
|
|
|
|
cluster.on('fork', (worker) => {
|
|
worker.on('message', (msg) => {
|
|
cluster.workers &&
|
|
Object.keys(cluster.workers).forEach((id) => {
|
|
cluster.workers?.[id]?.send(msg)
|
|
})
|
|
})
|
|
})
|
|
|
|
cluster.on('online', (worker) => {
|
|
consola.info('Worker %s is online', worker.process.pid)
|
|
})
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
if (code !== 0) {
|
|
consola.info(`Worker ${worker.process.pid} died. Restarting`)
|
|
cluster.fork()
|
|
}
|
|
})
|
|
} else {
|
|
callback()
|
|
}
|
|
}
|
|
}
|