-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (111 loc) · 3.41 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var express = require('express'),
cluster = require('cluster'),
net = require('net'),
sio = require('socket.io'),
sio_redis = require('socket.io-redis'),
init = require('./config/init')(),
config = require('./config/config'),
fs = require('fs')
;
var port = config.port;
var num_processes = 1;
if (process.env.NODE_ENV=='production'){
num_processes = process.env.WORKERS || require('os').cpus().length;
}
num_processes = 1;
var autoRestartedTimes = 0;
var worker_start_ts = [];
var worker_start_cnt = [];
if (cluster.isMaster) {
var workers = [];
//Helper function for spawning worker at index 'i'.
var spawn = function(i) {
workers[i] = cluster.fork();
worker_start_ts[i] = new Date().getTime();
worker_start_cnt[i]++;
// Optional: Restart worker on exit
workers[i].on('exit', function(worker, code, signal) {
logError("espawning worker "+ i , function(){
console.log('respawning worker', i);
var nowTs = new Date().getTime();
if( (nowTs-worker_start_ts[i])<1000 ){
worker_start_ts[i] = nowTs;
worker_start_cnt[i]++;
}
if( worker_start_cnt[i]<10){
spawn(i);
}else{
logError("Permanently Failed, too many restarting", function(){
console.log("permanently failed ");
});
}
} );
});
};
// Spawn workers.
for (var i = 0; i < num_processes; i++) {
worker_start_cnt[i] = 0;
spawn(i);
}
var worker_index = function(ip, len) {
var s = '';
for (var i = 0, _len = ip.length; i < _len; i++) {
if (ip[i] !== '.') {
s += ip[i];
}
}
return Number(s) % len;
};
// Create the outside facing server listening on our port.
if(num_processes>1)
var server = net.createServer(function(connection) {
var worker = workers[worker_index(connection.remoteAddress, num_processes)];
worker.send('sticky-session:connection', connection);
}).listen(port);
} else {
var mongoose = require('mongoose');
//Bootstrap db connection
var db = mongoose.connect(config.db, {server:{auto_reconnect:true}});
//Init the express application
var app = require('./config/express')(db);
//Start the app by listening on <port>
if(num_processes>1)
var localServer = app.listen(0, 'localhost');
else
var localServer = app.listen(port);
var io = sio(localServer);
if(num_processes>1)
io.adapter(sio_redis({ host: config.redis.host, port: config.redis.port }));
var socketService = require('./app/services/sockets');
socketService()['init'](io);
// Listen to messages sent from the master. Ignore everything else.
if(num_processes>1){
process.on('message', function(message, connection) {
if (message !== 'sticky-session:connection') {
return;
}
localServer.emit('connection', connection);
});
}
//Logging initialization
console.log('MEAN.JS application started on port ' + config.port);
}
process.on('uncaughtException', function (err) {
logError( err.message+'\n'+ err.stack, function(){
process.exit(1);
});
})
var logError=function(string, cb){
fs.open('error_log','a', function(err, fd){
if(err){
console.log(err);
}else{
var buffer = new Buffer( new Date().toUTCString() +":" + string +'\n'
);
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
fs.close(fd);
cb();
});
}
});
}