app_调试模式.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2015 MemDB.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. /*
  17. * quick pomelo template project
  18. *
  19. * start memdb first by:
  20. * memdbcluster start -c ./config/memdb.conf.js
  21. */
  22. var util = require('util');
  23. var pomelo = require('pomelo');
  24. var quick = require('quick-pomelo');
  25. var pomeloConstants = require('pomelo/lib/util/constants');
  26. var P = quick.Promise;
  27. var logger = quick.logger.getLogger('pomelo', __filename);
  28. var pomeloLogger = require('pomelo/node_modules/pomelo-logger');
  29. var app = pomelo.createApp();
  30. app.set('name', 'fhmj');
  31. // Configure for global
  32. app.configure('all', function () {
  33. app.enable('systemMonitor');
  34. app.set('proxyConfig', {
  35. bufferMsg: true,
  36. interval: 30,
  37. lazyConnection: true,
  38. timeout: 15 * 1000,
  39. failMode: 'failfast'
  40. });
  41. app.set('remoteConfig', {
  42. bufferMsg: true,
  43. interval: 30
  44. });
  45. // Configure memdb
  46. app.loadConfigBaseApp('memdbConfig', 'memdb.json');
  47. // Load components
  48. app.load(quick.components.memdb);
  49. app.load(quick.components.controllers);
  50. app.load(quick.components.routes);
  51. app.load(quick.components.timer);
  52. // Configure logger
  53. var loggerConfig = app.getBase() + '/config/log4js.json';
  54. var loggerOpts = {
  55. serverId: app.getServerId(),
  56. base: app.getBase()
  57. };
  58. quick.logger.configure(loggerConfig, loggerOpts);
  59. // Configure filter
  60. if (app.getServerType() != 'gate') app.filter(quick.filters.transaction(app));
  61. app.globalFilter(require('./globalFilter')(app));
  62. // Add beforeStop hook
  63. app.lifecycleCbs[pomeloConstants.LIFECYCLE.BEFORE_SHUTDOWN] = function (app, shutdown, cancelShutDownTimer) {
  64. cancelShutDownTimer();
  65. if (app.getServerType() === 'master') {
  66. // Wait for all server stop
  67. var tryShutdown = function () {
  68. if (Object.keys(app.getServers()).length === 0) {
  69. quick.logger.shutdown(shutdown);
  70. }
  71. else {
  72. setTimeout(tryShutdown, 200);
  73. }
  74. };
  75. tryShutdown();
  76. return;
  77. }
  78. quick.logger.shutdown(shutdown);
  79. };
  80. app.set('errorHandler', function (err, msg, resp, session, cb) {
  81. resp = {
  82. code: 500,
  83. stack: err.stack,
  84. message: err.message
  85. };
  86. cb(err, resp);
  87. });
  88. });
  89. // Gate settings
  90. app.configure('all', 'gate', function () {
  91. app.set('connectorConfig', {
  92. connector: pomelo.connectors.hybridconnector
  93. });
  94. app.set('sessionConfig', {
  95. singleSession: true
  96. });
  97. });
  98. //Connector settings
  99. app.configure('all', 'connector', function () {
  100. app.set('connectorConfig', {
  101. connector: pomelo.connectors.hybridconnector,
  102. heartbeat: 30,
  103. disconnectOnTimeout: true
  104. });
  105. app.set('sessionConfig', {
  106. singleSession: true
  107. });
  108. });
  109. // Http servers
  110. app.configure('all', 'http', function () {
  111. var script = app.getCurServer().script;
  112. app.load(require('./http/' + script));
  113. });
  114. // Game route
  115. app.configure('production|development', function () {
  116. app.route('game', require('./gameRoute').gameRoute);
  117. });
  118. app.configure('development', function () {
  119. require('heapdump');
  120. quick.Promise.longStackTraces();
  121. quick.logger.setGlobalLogLevel(quick.logger.levels.WARN);
  122. pomeloLogger.setGlobalLogLevel(pomeloLogger.levels.WARN);
  123. });
  124. app.configure('production', function () {
  125. quick.logger.setGlobalLogLevel(quick.logger.levels.WARN);
  126. pomeloLogger.setGlobalLogLevel(pomeloLogger.levels.WARN);
  127. });
  128. process.on('uncaughtException', function (err) {
  129. logger.error('Uncaught exception: %s', err.stack);
  130. });
  131. app.start();