remote.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Component for remote service.
  3. * Load remote service and add to global context.
  4. */
  5. var fs = require('fs');
  6. var pathUtil = require('../util/pathUtil');
  7. var RemoteServer = require('pomelo-rpc').server;
  8. /**
  9. * Remote component factory function
  10. *
  11. * @param {Object} app current application context
  12. * @param {Object} opts construct parameters
  13. * opts.acceptorFactory {Object}: acceptorFactory.create(opts, cb)
  14. * @return {Object} remote component instances
  15. */
  16. module.exports = function(app, opts) {
  17. opts = opts || {};
  18. // cacheMsg is deprecated, just for compatibility here.
  19. opts.bufferMsg = opts.bufferMsg || opts.cacheMsg || false;
  20. opts.interval = opts.interval || 30;
  21. if(app.enabled('rpcDebugLog')) {
  22. opts.rpcDebugLog = true;
  23. opts.rpcLogger = require('pomelo-logger').getLogger('rpc-debug', __filename);
  24. }
  25. return new Component(app, opts);
  26. };
  27. /**
  28. * Remote component class
  29. *
  30. * @param {Object} app current application context
  31. * @param {Object} opts construct parameters
  32. */
  33. var Component = function(app, opts) {
  34. this.app = app;
  35. this.opts = opts;
  36. };
  37. var pro = Component.prototype;
  38. pro.name = '__remote__';
  39. /**
  40. * Remote component lifecycle function
  41. *
  42. * @param {Function} cb
  43. * @return {Void}
  44. */
  45. pro.start = function(cb) {
  46. this.opts.port = this.app.getCurServer().port;
  47. this.remote = genRemote(this.app, this.opts);
  48. this.remote.start();
  49. process.nextTick(cb);
  50. };
  51. /**
  52. * Remote component lifecycle function
  53. *
  54. * @param {Boolean} force whether stop the component immediately
  55. * @param {Function} cb
  56. * @return {Void}
  57. */
  58. pro.stop = function(force, cb) {
  59. this.remote.stop(force);
  60. process.nextTick(cb);
  61. };
  62. /**
  63. * Get remote paths from application
  64. *
  65. * @param {Object} app current application context
  66. * @return {Array} paths
  67. *
  68. */
  69. var getRemotePaths = function(app) {
  70. var paths = [];
  71. var role;
  72. // master server should not come here
  73. if(app.isFrontend()) {
  74. role = 'frontend';
  75. } else {
  76. role = 'backend';
  77. }
  78. var sysPath = pathUtil.getSysRemotePath(role), serverType = app.getServerType();
  79. if(fs.existsSync(sysPath)) {
  80. paths.push(pathUtil.remotePathRecord('sys', serverType, sysPath));
  81. }
  82. var userPath = pathUtil.getUserRemotePath(app.getBase(), serverType);
  83. if(fs.existsSync(userPath)) {
  84. paths.push(pathUtil.remotePathRecord('user', serverType, userPath));
  85. }
  86. return paths;
  87. };
  88. /**
  89. * Generate remote server instance
  90. *
  91. * @param {Object} app current application context
  92. * @param {Object} opts contructor parameters for rpc Server
  93. * @return {Object} remote server instance
  94. */
  95. var genRemote = function(app, opts) {
  96. opts.paths = getRemotePaths(app);
  97. opts.context = app;
  98. if(!!opts.rpcServer) {
  99. return opts.rpcServer.create(opts);
  100. } else {
  101. return RemoteServer.create(opts);
  102. }
  103. };