-
Notifications
You must be signed in to change notification settings - Fork 4
/
master.js
40 lines (31 loc) · 971 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
37
38
39
40
/**
* master.js
* @description: main server process
* @author: Chris Young <[email protected]>
*/
var cluster = require('cluster'),
os = require('os');
var config = require(__dirname+'/config.json'),
worker = require(__dirname+'/worker.js');
var log = require(__dirname+'/library/log.js');
if (!cluster.isMaster)
return worker();
log.setup(function setup(exception) {
if (exception) {
console.log('failed to create logging directory');
process.exit(1);
}
log.info('application started');
console.log('application started on port:', config.port);
var cores = os.cpus().length;
for (var x = 0; x < cores; x++)
cluster.fork();
cluster.on('exit', function exit(worker, code, signal) {
log.error('worker'+worker.process.pid+' exited', code || signal );
if (code != -1)
cluster.fork();
});
cluster.on('online', function online(worker) {
log.info('worker'+worker.process.pid+' came online');
});
});