Skip to content

Commit

Permalink
chore(master): admin
Browse files Browse the repository at this point in the history
管理员代码重构
  • Loading branch information
huangyoukun authored and RobinzZH committed May 28, 2018
1 parent c1f96db commit 2f64c10
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 97 deletions.
97 changes: 97 additions & 0 deletions bin/proxy/admin.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*!
* Tencent is pleased to support the open source community by making Tencent Server Web available.
* Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

'use strict';

const logger = require('logger');
const cp = require('child_process');

module.exports = {
'default': function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('no such command!');
},

'/globaldump': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'globaldump',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/heapdump': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'heapdump',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/profiler': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'profiler',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/top100': function (req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'top100',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/reload': function (req, res) {

cp.exec('./check.js', {
timeout: 5000,
cwd: __dirname
}, function (err, stdout, stderr) {
let hasError = false;
if (err) {
hasError = true;
logger.error(err.stack);
}

if (stderr && stderr.length > 0) {
hasError = true;
logger.error(stderr.toString('UTF-8'));
}

if (hasError) {
//异常情况下,不能直接res.end(),需要模拟http异常,让shell感知到异常
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.write(stderr.toString('UTF-8'));

res.socket && res.socket.end();
return;
}

if (stdout && stdout.length > 0) {
logger.info(stdout.toString('UTF-8'));

//开始reload
process.emit('reload', req.GET);

res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.write(stderr.toString('UTF-8'));
res.end('\r\ndone!\r\n');

return;
}

});
}
};
105 changes: 8 additions & 97 deletions bin/proxy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,21 @@ const config = require('./config.js');
const http = require('http');
const codeWatch = require('api/code/watcher.js');
const parseGet = require('util/http/parseGet.js');
const cp = require('child_process');
const actions = require('./admin.actions.js');

const server = http.createServer(function(req, res) {

let action;

logger.info('admin request by: ${url}', {
url: req.url
});

//解析get参数
parseGet(req);

action = methodMap[req.REQUEST.pathname] || methodMap['default'];

action.apply(methodMap, arguments);

parseGet(req); //解析get参数
action = actions[req.REQUEST.pathname] || actions['default'];
action.call(actions, req, res);
});

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

server.listen(config.httpAdminPort, '127.0.0.1', function(err) {
if(err) {
Expand All @@ -48,92 +43,8 @@ server.listen(config.httpAdminPort, '127.0.0.1', function(err) {
}
});

//管理进程开启debug日志
logger.setLogLevel('debug');

const methodMap = {

'default' : function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('no such command!');
},

'/globaldump' : function(req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'globaldump',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/heapdump' : function(req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'heapdump',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/profiler' : function(req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'profiler',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/top100' : function(req, res) {
process.emit('sendCmd2workerOnce', {
CMD: 'top100',
GET: req.GET
});
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.end('done!\r\n');
},

'/reload' : function(req, res) {

cp.exec('./check.js', {
timeout: 5000,
cwd: __dirname
}, function(err, stdout, stderr) {
if(err) {
logger.error(err.stack);
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.write(err.stack);
res.socket && res.socket.end();

return;
}

if(stderr && stderr.length > 0) {

logger.error(stderr.toString('UTF-8'));
res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.write(stderr.toString('UTF-8'));

res.socket && res.socket.end();
return;
}

if(stdout && stdout.length > 0) {
logger.info(stdout.toString('UTF-8'));

process.emit('reload', req.GET);

res.writeHead(200, {'Content-Type': 'text/plain; charset=UTF-8'});
res.write(stderr.toString('UTF-8'));
res.end('\r\ndone!\r\n');

return;
}

});

}

};

logger.setLogLevel(0);
//变更感知
codeWatch.watch();

0 comments on commit 2f64c10

Please sign in to comment.