-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (48 loc) · 1.46 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var pjson = require("./package.json");
var config = require("./config/general");
var socket_config = require("./config/general");
var log = require("winston")
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var Server = require("./scripts/server");
var Socket = require("./scripts/socket");
var Cluster = function(cluster) {
var exports = {
master: cluster.isMaster
};
function start(cb) {
cb = cb || function(){};
if (cluster.isMaster) {
log.info((pjson.name || "web cluster") + " > starting infrastructure...");
for (var i = 0; i < numCPUs; i++)
cluster.fork()
exports.cpus = numCPUs;
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
cb();
}
else if (cluster.isWorker) {
var socket = null;
var server = new Server(function(server, app){
server.listen(app.get('port'), function(){
socket = new Socket(server);
log.info('worker listening on port ' + app.get('port'));
});
});
}
}; exports.start = start;
function init() {
if(!config[config.state])
throw new Error("No configuration state available");
var state = config[config.state];
numCPUs = state.cluster? state.cluster.max || numCPUs : numCPUs;
return exports;
}
return init();
}
var cluster = new Cluster(cluster);
cluster.start(function(){
log.info("starting cluster with " + cluster.cpus + " forks.\n")
});