monitor.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Component for monitor.
  3. * Load and start monitor client.
  4. */
  5. var logger = require('pomelo-logger').getLogger('pomelo', __filename);
  6. var admin = require('pomelo-admin');
  7. var moduleUtil = require('../util/moduleUtil');
  8. var utils = require('../util/utils');
  9. var Constants = require('../util/constants');
  10. var Monitor = function(app, opts) {
  11. opts = opts || {};
  12. this.app = app;
  13. this.serverInfo = app.getCurServer();
  14. this.masterInfo = app.getMaster();
  15. this.modules = [];
  16. this.closeWatcher = opts.closeWatcher;
  17. this.monitorConsole = admin.createMonitorConsole({
  18. id: this.serverInfo.id,
  19. type: this.app.getServerType(),
  20. host: this.masterInfo.host,
  21. port: this.masterInfo.port,
  22. info: this.serverInfo,
  23. env: this.app.get(Constants.RESERVED.ENV),
  24. authServer: app.get('adminAuthServerMonitor') // auth server function
  25. });
  26. };
  27. module.exports = Monitor;
  28. Monitor.prototype.start = function(cb) {
  29. moduleUtil.registerDefaultModules(false, this.app, this.closeWatcher);
  30. this.startConsole(cb);
  31. };
  32. Monitor.prototype.startConsole = function(cb) {
  33. moduleUtil.loadModules(this, this.monitorConsole);
  34. var self = this;
  35. this.monitorConsole.start(function(err) {
  36. if (err) {
  37. utils.invokeCallback(cb, err);
  38. return;
  39. }
  40. moduleUtil.startModules(self.modules, function(err) {
  41. utils.invokeCallback(cb, err);
  42. return;
  43. });
  44. });
  45. this.monitorConsole.on('error', function(err) {
  46. if(!!err) {
  47. logger.error('monitorConsole encounters with error: %j', err.stack);
  48. return;
  49. }
  50. });
  51. };
  52. Monitor.prototype.stop = function(cb) {
  53. this.monitorConsole.stop();
  54. this.modules = [];
  55. process.nextTick(function() {
  56. utils.invokeCallback(cb);
  57. });
  58. };
  59. // monitor reconnect to master
  60. Monitor.prototype.reconnect = function(masterInfo) {
  61. var self = this;
  62. this.stop(function() {
  63. self.monitorConsole = admin.createMonitorConsole({
  64. id: self.serverInfo.id,
  65. type: self.app.getServerType(),
  66. host: masterInfo.host,
  67. port: masterInfo.port,
  68. info: self.serverInfo,
  69. env: self.app.get(Constants.RESERVED.ENV)
  70. });
  71. self.startConsole(function() {
  72. logger.info('restart modules for server : %j finish.', self.app.serverId);
  73. });
  74. });
  75. };