master.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var starter = require('./starter');
  2. var logger = require('pomelo-logger').getLogger('pomelo', __filename);
  3. var crashLogger = require('pomelo-logger').getLogger('crash-log', __filename);
  4. var adminLogger = require('pomelo-logger').getLogger('admin-log', __filename);
  5. var admin = require('pomelo-admin');
  6. var util = require('util');
  7. var utils = require('../util/utils');
  8. var moduleUtil = require('../util/moduleUtil');
  9. var Constants = require('../util/constants');
  10. var Server = function(app, opts) {
  11. this.app = app;
  12. this.masterInfo = app.getMaster();
  13. this.registered = {};
  14. this.modules = [];
  15. opts = opts || {};
  16. opts.port = this.masterInfo.port;
  17. opts.env = this.app.get(Constants.RESERVED.ENV);
  18. this.closeWatcher = opts.closeWatcher;
  19. this.masterConsole = admin.createMasterConsole(opts);
  20. };
  21. module.exports = Server;
  22. Server.prototype.start = function(cb) {
  23. moduleUtil.registerDefaultModules(true, this.app, this.closeWatcher);
  24. moduleUtil.loadModules(this, this.masterConsole);
  25. var self = this;
  26. // start master console
  27. this.masterConsole.start(function(err) {
  28. if(err) {
  29. process.exit(0);
  30. }
  31. moduleUtil.startModules(self.modules, function(err) {
  32. if(err) {
  33. utils.invokeCallback(cb, err);
  34. return;
  35. }
  36. if(self.app.get(Constants.RESERVED.MODE) !== Constants.RESERVED.STAND_ALONE) {
  37. starter.runServers(self.app);
  38. }
  39. utils.invokeCallback(cb);
  40. });
  41. });
  42. this.masterConsole.on('error', function(err) {
  43. if(!!err) {
  44. logger.error('masterConsole encounters with error: ' + err.stack);
  45. return;
  46. }
  47. });
  48. this.masterConsole.on('reconnect', function(info){
  49. self.app.addServers([info]);
  50. });
  51. // monitor servers disconnect event
  52. this.masterConsole.on('disconnect', function(id, type, info, reason) {
  53. crashLogger.info(util.format('[%s],[%s],[%s],[%s]', type, id, Date.now(), reason || 'disconnect'));
  54. var count = 0;
  55. var time = 0;
  56. var pingTimer = null;
  57. var server = self.app.getServerById(id);
  58. var stopFlags = self.app.get(Constants.RESERVED.STOP_SERVERS) || [];
  59. if(!!server && (server[Constants.RESERVED.AUTO_RESTART] === 'true' || server[Constants.RESERVED.RESTART_FORCE] === 'true') && stopFlags.indexOf(id) < 0) {
  60. var setTimer = function(time) {
  61. pingTimer = setTimeout(function() {
  62. utils.ping(server.host, function(flag) {
  63. if(flag) {
  64. handle();
  65. } else {
  66. count++;
  67. if(count > 3) {
  68. time = Constants.TIME.TIME_WAIT_MAX_PING;
  69. } else {
  70. time = Constants.TIME.TIME_WAIT_PING * count;
  71. }
  72. setTimer(time);
  73. }
  74. });
  75. }, time);
  76. };
  77. setTimer(time);
  78. var handle = function() {
  79. clearTimeout(pingTimer);
  80. utils.checkPort(server, function(status) {
  81. if(status === 'error') {
  82. utils.invokeCallback(cb, new Error('Check port command executed with error.'));
  83. return;
  84. } else if(status === 'busy') {
  85. if(!!server[Constants.RESERVED.RESTART_FORCE]) {
  86. starter.kill([info.pid], [server]);
  87. } else {
  88. utils.invokeCallback(cb, new Error('Port occupied already, check your server to add.'));
  89. return;
  90. }
  91. }
  92. setTimeout(function() {
  93. starter.run(self.app, server, null);
  94. }, Constants.TIME.TIME_WAIT_STOP);
  95. });
  96. };
  97. }
  98. });
  99. // monitor servers register event
  100. this.masterConsole.on('register', function(record) {
  101. starter.bindCpu(record.id, record.pid, record.host);
  102. });
  103. this.masterConsole.on('admin-log', function(log, error) {
  104. if(error) {
  105. adminLogger.error(JSON.stringify(log));
  106. } else {
  107. adminLogger.info(JSON.stringify(log));
  108. }
  109. });
  110. };
  111. Server.prototype.stop = function(cb) {
  112. this.masterConsole.stop();
  113. process.nextTick(cb);
  114. };