-
Notifications
You must be signed in to change notification settings - Fork 255
/
dispatch.js
30 lines (26 loc) · 1.18 KB
/
dispatch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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) {
const maxSize = +process.env.SIGMA_MAX_PROCESSORS_LIMIT || +process.env.AJDK_MAX_PROCESSORS_LIMIT || require('os').cpus().length
for (let i = 0; i < maxSize; i++) {
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()
})
}