app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2015 rain1017.
  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 --conf=./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', 'quick-pomelo');
  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 : 10 * 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. app.filter(quick.filters.transaction(app));
  61. app.globalFilter(quick.filters.reqId(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. //Connector settings
  90. app.configure('all', 'gate|connector', function() {
  91. app.set('connectorConfig', {
  92. connector : pomelo.connectors.hybridconnector,
  93. heartbeat : 30,
  94. });
  95. app.set('sessionConfig', {
  96. singleSession : true,
  97. });
  98. });
  99. app.configure('development', function(){
  100. require('heapdump');
  101. quick.Promise.longStackTraces();
  102. quick.logger.setGlobalLogLevel(quick.logger.levels.DEBUG);
  103. pomeloLogger.setGlobalLogLevel(pomeloLogger.levels.DEBUG);
  104. });
  105. app.configure('production', function(){
  106. quick.logger.setGlobalLogLevel(quick.logger.levels.WARN);
  107. pomeloLogger.setGlobalLogLevel(pomeloLogger.levels.WARN);
  108. });
  109. process.on('uncaughtException', function(err) {
  110. logger.error('Uncaught exception: %s', err.stack);
  111. });
  112. app.start();