-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (41 loc) · 1.51 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var cluster = require('cluster'),
os = require('os'),
getConfig = require('./lib/utilities/get-config');
var express = require('express'),
winston = require('winston'),
chalk = require('chalk'),
logLevel = ( process.env.environment === 'development' || process.env.environment === 'dev' ) ? 'debug' : 'info';
winston.loggers.add('default', {
transports: [
new ( winston.transports.Console )({ level: logLevel })
]
});
var port = getConfig('port') || 3000,
socketPort = getConfig('socketPort') || 3001;
if( cluster.isMaster ) {
var workers = [];
os.cpus().forEach(function ( cpu, cpuIndex ) {
function boot ( i ) {
workers[ i ] = cluster.fork();
workers[ i ].on('exit', function () {
winston.error(chalk.bgRed('Worker died. :( RIP Worker', i, '. Rebooting...'));
boot(i);
});
}
boot( cpuIndex );
});
var SocketServer = require('./lib/socket/server'),
server = require('http').Server(express()),
socketServer = new SocketServer(server);
server.listen(socketPort, function () {
winston.info(chalk.dim('Socket Server listening on port:', port));
});
} else {
winston.info(chalk.dim('[', cluster.worker.id, '] Starting worker ...'));
process.title = 'Trust API Worker - ' + cluster.worker.id + ' - Node.js';
var app = require('./app');
app.registerModels();
app.init(express()).listen(port, function () {
winston.info(chalk.dim('[', cluster.worker.id, '] Worker listening on port:', port));
});
}