forked from chris--young/Slacker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.js
36 lines (29 loc) · 908 Bytes
/
master.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
32
33
34
35
36
var cluster = require('cluster')
var os = require('os')
var config = require(__dirname + '/library/config')
var worker = require(__dirname + '/worker.js')
var log = require(__dirname + '/library/log.js')
if (cluster.isMaster) {
log.setup(function(exception) {
if (exception) {
console.log('failed to create logging directory')
process.exit(1)
}
log.info('application started')
console.log('application started on port:', process.env.PORT || config.port)
var cores = os.cpus().length
for (var x = 0; x < cores; x++)
cluster.fork()
cluster.on('exit', function(worker, code, signal) {
log.error('worker' + worker.process.pid + ' exited', code || signal )
if (code !== -1)
cluster.fork()
});
cluster.on('online', function(worker) {
log.info('worker' + worker.process.pid + ' came online')
})
})
}
else {
worker()
}