-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
31 lines (24 loc) · 836 Bytes
/
app.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
31
var _ = require('lodash')
, config = require('./config')
, cluster = require('cluster')
, os = require('os')
, numCPUs = os.cpus().length
, totalInstances = config.cluster.instances || numCPUs
require('remedial')
if (cluster.isMaster) {
console.log('Creating {n} workers'.supplant({ n: totalInstances }))
_(totalInstances).times(function() {
cluster.fork()
})
cluster.on('exit', function(worker, code, signal) {
var workerProcess = worker.process
, pid = workerProcess.pid
, exitCode = workerProcess.exitCode
console.log('worker@{pid}#{id} died (with exitcode {exitCode}). Restarting...'.supplant({ pid: pid, id: worker.id, exitCode: exitCode }))
cluster.fork()
})
} else {
var worker = cluster.worker
console.log('Starting app on port ' + config.port)
require('./server')
}