-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
194 lines (176 loc) · 5.73 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
// var numCPUs = 4;
var heapdump = require('heapdump');
var _ = require('underscore');
var domain = require('domain');
var toobusy = require('toobusy');
var async = require('async');
var mongoose = require('mongoose');
var common = require('./libs/common.js');
var Router = require("./libs/router.js");
var RoomManager = require('./roommanager.js');
var socket = require('./libs/streamedsocket.js');
var CpuCare = require('./libs/cpucare.js');
var logger = common.logger;
var globalConf = common.globalConf;
// var express = require('express');
// var httpServer = express();
// var agent = require('webkit-devtools-agent');
if (cluster.isMaster) {
var localSocket = null;
var router = null;
process.title = 'painttyServer master';
async.auto({
'init_db': function(callback) {
mongoose.connect(globalConf['database']['connectionString']);
var db = mongoose.connection;
db.on('error', function(er) {
logger.error('connection error:', er);
callback(er);
});
db.once('open', function () {
callback();
});
},
'init_local_socket': ['init_db', function(callback){
router = new Router();
router.reg('request', 'broadcast', function(cli, obj){
var msg = obj['msg'];
if (!_.isString(msg)) {
var ret = {
'response': 'broadcast',
'result': false
};
return;
};
var send_to_workers = {
message: 'broadcast',
content: msg
};
var w = cluster.workers;
if (w[0]) {
w[0].send(send_to_workers);
};
var ret = {
'response': 'broadcast',
'result': true
};
var jsString = common.jsonToString(ret);
cli.sendData(cli, new Buffer(jsString));
});
localSocket = new socket.SocketServer({'record': false, 'keepAlive': false});
localSocket.on('message', function(client, data) {
var obj = common.stringToJson(data);
logger.log(obj);
self.router.message(client, obj);
}).on('error', function(err){
logger.error(err);
});
localSocket.listen('56565', '127.0.0.1');
callback();
}],
'fork_child': ['init_local_socket', function(callback){
// Fork workers.
function forkWorker(memberId) {
var worker = cluster.fork({'memberId': memberId});
worker.memberId = memberId;
worker.on('message', function(msg) {
_.each(cluster.workers, function(ele, index, list) {
ele.send(msg);
});
});
// attach cpu usage monitor
var c_care = new CpuCare({
target: worker.process.pid,
mark: 90,
cycle: 5*60*1000 // check per 5 min
});
c_care.once('processkilled', function(killed_info) {
logger.warn('worker is killed because of cpu usage', killed_info);
});
}
for (var i = 0; i < numCPUs; i++) {
forkWorker(i);
}
cluster.on('exit', function(worker, code, signal) {
if(worker.memberId){
logger.error('Worker with memberId', worker.memberId, 'died');
forkWorker(worker.memberId);
}
});
callback();
}]
}, function(er, re){
if (er) {
logger.error('Error while init master process: ', er);
};
});
} else {
var d1 = domain.create();
var roomManager;
d1.run(function() {
var memberId = 0;
if (process.env['memberId']) {
memberId = parseInt(process.env['memberId'], 10);
}else{
logger.error('Worker process inited without memberId!');
}
process.title = 'painttyServer child, memberId:' + memberId;
roomManager = new RoomManager({
localId: memberId,
name: 'rmmgr',
pubPort: globalConf['manager']['publicPort']
});
process.on('SIGINT', function() {
if (roomManager) {
roomManager.stop();
};
toobusy.shutdown();
process.exit();
});
});
d1.on('error', function(er1) {
logger.error('Error with RoomManager:', er1);
if(er1.code == 'EADDRINUSE'){
return;
}
try {
// make sure we close down within 30 seconds
var killtimer = setTimeout(function() {
process.exit(1);
}, 30000);
// But don't keep the process open just for that!
killtimer.unref();
if (roomManager) {
var error_notify = '<p style="font-weight:bold;color:red;">完蛋了!!'+
'检测到服务端发生了一些故障,赶快逃离吧!!。</p>\n';
roomManager.localcast(error_notify);
roomManager.stop();
};
} catch(er) {
logger.error('Cannot gently close RoomManager:', err);
toobusy.shutdown();
process.exit(1);
}
});
}
// httpServer.get('/', function(req, res) {
// var list = [];
// _.each(roomManager.roomObjs, function(item) {
// if(_.isUndefined(item)) return;
// var r = {
// cmdport: item.cmdSocket.address().port,
// // serveraddress: roomManager.pubServer.address().address,
// maxload: item.options.maxLoad,
// currentload: item.currentLoad(),
// name: item.options.name,
// 'private': item.options.password.length > 0
// };
// list.push(r);
// });
// list.push(roomManager.pubServer.address());
// var m = JSON.stringify(list);
// res.send('<h2>Hello from Mr.Paint</h2><p>Here is some debug info: </p>'+m);
// });
// httpServer.listen(39797);