pushScheduler.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Scheduler component to schedule message sending.
  3. */
  4. var DefaultScheduler = require('../pushSchedulers/direct');
  5. var logger = require('pomelo-logger').getLogger('pomelo', __filename);
  6. module.exports = function(app, opts) {
  7. return new PushScheduler(app, opts);
  8. };
  9. var PushScheduler = function(app, opts) {
  10. this.app = app;
  11. opts = opts || {};
  12. this.scheduler = getScheduler(this, app, opts);
  13. };
  14. PushScheduler.prototype.name = '__pushScheduler__';
  15. /**
  16. * Component lifecycle callback
  17. *
  18. * @param {Function} cb
  19. * @return {Void}
  20. */
  21. PushScheduler.prototype.afterStart = function(cb) {
  22. if(this.isSelectable) {
  23. for (var k in this.scheduler) {
  24. var sch = this.scheduler[k];
  25. if(typeof sch.start === 'function') {
  26. sch.start();
  27. }
  28. }
  29. process.nextTick(cb);
  30. } else if(typeof this.scheduler.start === 'function') {
  31. this.scheduler.start(cb);
  32. } else {
  33. process.nextTick(cb);
  34. }
  35. };
  36. /**
  37. * Component lifecycle callback
  38. *
  39. * @param {Function} cb
  40. * @return {Void}
  41. */
  42. PushScheduler.prototype.stop = function(force, cb) {
  43. if(this.isSelectable) {
  44. for (var k in this.scheduler) {
  45. var sch = this.scheduler[k];
  46. if(typeof sch.stop === 'function') {
  47. sch.stop();
  48. }
  49. }
  50. process.nextTick(cb);
  51. } else if(typeof this.scheduler.stop === 'function') {
  52. this.scheduler.stop(cb);
  53. } else {
  54. process.nextTick(cb);
  55. }
  56. };
  57. /**
  58. * Schedule how the message to send.
  59. *
  60. * @param {Number} reqId request id
  61. * @param {String} route route string of the message
  62. * @param {Object} msg message content after encoded
  63. * @param {Array} recvs array of receiver's session id
  64. * @param {Object} opts options
  65. * @param {Function} cb
  66. */
  67. PushScheduler.prototype.schedule = function(reqId, route, msg, recvs, opts, cb) {
  68. var self = this;
  69. if(self.isSelectable) {
  70. if(typeof self.selector === 'function') {
  71. self.selector(reqId, route, msg, recvs, opts, function(id) {
  72. if(self.scheduler[id] && typeof self.scheduler[id].schedule === 'function') {
  73. self.scheduler[id].schedule(reqId, route, msg, recvs, opts, cb);
  74. } else {
  75. logger.error('invalid pushScheduler id, id: %j', id);
  76. }
  77. });
  78. } else {
  79. logger.error('the selector for pushScheduler is not a function, selector: %j', self.selector);
  80. }
  81. } else {
  82. if (typeof self.scheduler.schedule === 'function') {
  83. self.scheduler.schedule(reqId, route, msg, recvs, opts, cb);
  84. } else {
  85. logger.error('the scheduler does not have a schedule function, scheduler: %j', self.scheduler);
  86. }
  87. }
  88. };
  89. var getScheduler = function(pushSchedulerComp, app, opts) {
  90. var scheduler = opts.scheduler || DefaultScheduler;
  91. if(typeof scheduler === 'function') {
  92. return scheduler(app, opts);
  93. }
  94. if(Array.isArray(scheduler)) {
  95. var res = {};
  96. scheduler.forEach(function(sch) {
  97. if(typeof sch.scheduler === 'function') {
  98. res[sch.id] = sch.scheduler(app, sch.options);
  99. } else {
  100. res[sch.id] = sch.scheduler;
  101. }
  102. });
  103. pushSchedulerComp.isSelectable = true;
  104. pushSchedulerComp.selector = opts.selector;
  105. return res;
  106. }
  107. return scheduler;
  108. };