base.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. module.exports = class BaseStrategy {
  3. constructor(schedule, agent, key) {
  4. this.agent = agent;
  5. this.key = key;
  6. this.schedule = schedule;
  7. this.logger = this.agent.getLogger('scheduleLogger');
  8. this.count = 0;
  9. }
  10. start() {}
  11. onJobStart() {}
  12. onJobFinish() {}
  13. /**
  14. * trigger one worker
  15. *
  16. * @param {...any} args - pass to job task
  17. */
  18. sendOne(...args) {
  19. /* istanbul ignore next */
  20. if (this.agent.schedule.closed) {
  21. this.logger.warn(`${this.key} skip due to schedule closed`);
  22. return;
  23. }
  24. this.count++;
  25. const info = {
  26. key: this.key,
  27. id: this.getSeqId(),
  28. args,
  29. };
  30. this.logger.debug(`[Job#${info.id}] ${info.key} triggered, send random by agent`);
  31. this.agent.messenger.sendRandom('egg-schedule', info);
  32. this.onJobStart(info);
  33. }
  34. /**
  35. * trigger all worker
  36. *
  37. * @param {...any} args - pass to job task
  38. */
  39. sendAll(...args) {
  40. /* istanbul ignore next */
  41. if (this.agent.schedule.closed) {
  42. this.logger.warn(`${this.key} skip due to schedule closed`);
  43. return;
  44. }
  45. this.count++;
  46. const info = {
  47. key: this.key,
  48. id: this.getSeqId(),
  49. args,
  50. };
  51. this.logger.debug(`[Job#${info.id}] ${info.key} triggered, send all by agent`);
  52. this.agent.messenger.send('egg-schedule', info);
  53. this.onJobStart(info);
  54. }
  55. getSeqId() {
  56. return `${Date.now()}${process.hrtime().join('')}${this.count}`;
  57. }
  58. };