Skip to content

Commit

Permalink
chore(server): exit while listening failure
Browse files Browse the repository at this point in the history
  • Loading branch information
huangyoukun committed Dec 13, 2018
1 parent 160f3be commit 774f626
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 51 deletions.
33 changes: 18 additions & 15 deletions bin/proxy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const parseGet = require('util/http/parseGet.js');
const actions = require('./admin.actions.js');

const server = http.createServer(function (req, res) {
logger.info('admin request by: ${url}', {
logger.debug('admin request by: ${url}', {
url: req.url
});

Expand All @@ -25,24 +25,27 @@ const server = http.createServer(function (req, res) {
action.call(actions, req, res);
});

server.on('error', serverError);

function serverError(err) {
logger.error('exit with ' + err.stack);

setTimeout(function() {
process.exit(1);
}, 500);
}

this.start = function () {
// 管理进程开启debug日志
logger.setLogLevel('debug');
// 管理进程开启info日志
logger.setLogLevel('info');

logger.info('start admin...');

server.listen(config.httpAdminPort, config.httpAdminAddress, function (err) {
if (err) {
logger.info('admin listen error ${address}:${port}', {
address: '127.0.0.1',
port: config.httpAdminPort
});
} else {
logger.info('admin listen ok ${address}:${port}', {
address: '127.0.0.1',
port: config.httpAdminPort
});
}
server.listen(config.httpAdminPort, config.httpAdminAddress, function () {
logger.info('admin listen ok ${address}:${port}', {
address: '127.0.0.1',
port: config.httpAdminPort
});
});

// 变更感知
Expand Down
36 changes: 14 additions & 22 deletions bin/proxy/http.proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ let serverHttps;
const server = http.createServer(requestHandler);
const serverThis = http.createServer(requestHandler);

server.on('error', serverError);
serverThis.on('error', serverError);

server.timeout = Math.max(config.timeout.upload || config.timeout.socket, 0);
serverThis.timeout = Math.max(config.timeout.upload || config.timeout.socket, 0);
server.keepAliveTimeout = Math.max(config.timeout.keepAlive, 0);
Expand All @@ -58,6 +61,7 @@ if (config.httpsOptions) {
requestHandler(req, res);
});

serverHttps.on('error', serverError);
serverHttps.on('clientError', function(err, socket) { // eslint-disable-line handle-callback-err
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
Expand Down Expand Up @@ -247,17 +251,7 @@ function listen(cpu) {
host: config.httpAddress,
port: config.httpPort,
exclusive: false
}, function(err) {
if (err) {
logger.info('cpu: ${cpu}, listen http error ${address}:${port}', {
cpu: serverInfo.cpu,
address: config.httpAddress,
port: config.httpPort
});

return;
}

}, function() {
logger.info('cpu: ${cpu}, listen http ok ${address}:${port}', {
cpu: serverInfo.cpu,
address: config.httpAddress,
Expand Down Expand Up @@ -319,17 +313,7 @@ function listen(cpu) {
if (serverHttps) {

// 启动https
serverHttps.listen(config.httpsPort, config.httpsAddress, function(err) {
if (err) {
logger.info('cpu: ${cpu}, listen https error ${address}:${port}', {
cpu: serverInfo.cpu,
address: config.httpsPort,
port: config.httpsAddress
});

return;
}

serverHttps.listen(config.httpsPort, config.httpsAddress, function() {
logger.info('cpu: ${cpu}, listen https ok ${address}:${port}', {
cpu: serverInfo.cpu,
address: config.httpsAddress,
Expand Down Expand Up @@ -393,6 +377,14 @@ function heartBeat() {
tnm2.Attr_API_Set('AVG_TSW_MEMORY_EXTERNAL', currMemory.external);
}

function serverError(err) {
logger.error('exit with ' + err.stack);

setTimeout(function() {
process.exit(1);
}, 500);
}


function afterCpu80(cpuUsed) {
// 取进程快照
Expand Down
10 changes: 10 additions & 0 deletions bin/proxy/kill.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ else
echo "do nothing, there is no pids"
fi

PIDS=$(ps aux|grep master-monitor|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')

if [ "$PIDS" ]
then
echo "kill -9 $PIDS"
kill -9 $PIDS
else
echo "do nothing, there is no pids"
fi

sleep 0.5s
echo "sleep 0.5s"

Expand Down
2 changes: 2 additions & 0 deletions bin/proxy/master-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ function startMonitor() {

logger.info(`master monitor started. pid:${masterPid}`);

process.title = 'TSW/worker/master-monitor';

if (!masterPid) {
logger.info('master pid is empty! exit monitor');
return;
Expand Down
2 changes: 1 addition & 1 deletion bin/proxy/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function startServer() {
}, 30 * 60000);

if (cluster.isMaster && debugOptions.inspectorEnabled) {
logger.setLogLevel('debug');
logger.setLogLevel('info');
logger.info('inspectorEnabled, start listening');
process.emit('message', {
cmd: 'listen', cpu: 0
Expand Down
30 changes: 17 additions & 13 deletions bin/proxy/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,7 @@ function bind_listen(server) {
wsRoute.doRoute(ws, 'message', { message: message, wsServer: server });
reportWebSocketLog(ws);
});
ws.on('close', function(code, reason) {
tnm2.Attr_API('SUM_TSW_WEBSOCKET_CLOSE', 1);
if (wsClient) {
wsClient.close();
return;
}
wsRoute.doRoute(ws, 'close', { code: code, reason: reason, wsServer: server });
logger.debug('websocket server close, code : ${code}, reason : ${reason}', {
code,
reason
});

ws.once('clear', function() {
// 清理domain
setTimeout(function() {
reportWebSocketLog(ws, true);
Expand All @@ -251,7 +240,21 @@ function bind_listen(server) {
logger.debug('cleared');
}, 3000);
});
ws.on('error', function(error) {
ws.once('close', function(code, reason) {
tnm2.Attr_API('SUM_TSW_WEBSOCKET_CLOSE', 1);
if (wsClient) {
wsClient.close();
return;
}
wsRoute.doRoute(ws, 'close', { code: code, reason: reason, wsServer: server });
logger.debug('websocket server close, code : ${code}, reason : ${reason}', {
code,
reason
});

this.emit('clear');
});
ws.once('error', function(error) {
tnm2.Attr_API('SUM_TSW_WEBSOCKET_ERROR', 1);
if (wsClient) {
if (wsClient.readyState === WebSocket.OPEN) {
Expand All @@ -265,6 +268,7 @@ function bind_listen(server) {
});

reportWebSocketLog(ws);
this.emit('clear');
});

logReport();
Expand Down

0 comments on commit 774f626

Please sign in to comment.