You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.1 KiB
JavaScript
30 lines
1.1 KiB
JavaScript
process.env.NODE_ENV = 'production'
|
|
|
|
// https://nodejs.org/api/cluster.html
|
|
// https://github.com/node-modules/graceful/blob/master/example/express_with_cluster/dispatch.js
|
|
// http://gitlab.alibaba-inc.com/mm/fb/blob/master/dispatch.js
|
|
|
|
let cluster = require('cluster')
|
|
let path = require('path')
|
|
let now = () => new Date().toISOString().replace(/T/, ' ').replace(/Z/, '')
|
|
|
|
cluster.setupMaster({
|
|
exec: path.join(__dirname, 'scripts/worker.js')
|
|
})
|
|
|
|
if (cluster.isMaster) {
|
|
require('os').cpus().forEach((cpu, index) => {
|
|
cluster.fork()
|
|
})
|
|
cluster.on('listening', (worker, address) => {
|
|
console.error(`[${now()}] master#${process.pid} worker#${worker.process.pid} is now connected to ${address.address}:${address.port}.`)
|
|
})
|
|
cluster.on('disconnect', (worker) => {
|
|
console.error(`[${now()}] master#${process.pid} worker#${worker.process.pid} has disconnected.`)
|
|
})
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
console.error(`[${now()}] master#${process.pid} worker#${worker.process.pid} died (${signal || code}). restarting...`)
|
|
cluster.fork()
|
|
})
|
|
}
|