simpleTrigger.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * This is the tirgger that use an object as trigger.
  3. */
  4. var SKIP_OLD_JOB = false;
  5. /**
  6. * The constructor of simple trigger
  7. */
  8. var SimpleTrigger = function(trigger, job){
  9. this.nextTime = (!!trigger.start)?trigger.start:Date.now();
  10. //The rec
  11. this.period = (!!trigger.period)?trigger.period:-1;
  12. //The running count of the job, -1 means no limit
  13. this.count = (!!trigger.count)?trigger.count:-1;
  14. this.job = job;
  15. };
  16. var pro = SimpleTrigger.prototype;
  17. /**
  18. * Get the current excuteTime of rigger
  19. */
  20. pro.excuteTime = function(){
  21. return this.nextTime;
  22. };
  23. /**
  24. * Get the next excuteTime of the trigger, and set the trigger's excuteTime
  25. * @return Next excute time
  26. */
  27. pro.nextExcuteTime = function(){
  28. var period = this.period;
  29. if((this.count > 0 && this.count <= this.job.runTime) || period <= 0)
  30. return null;
  31. this.nextTime += period;
  32. if(SKIP_OLD_JOB && this.nextTime < Date.now()){
  33. this.nextTime += Math.floor((Date.now()-this.nextTime)/period) * period;
  34. }
  35. return this.nextTime;
  36. };
  37. /**
  38. * Create Simple trigger
  39. */
  40. function createTrigger(trigger, job){
  41. return new SimpleTrigger(trigger, job);
  42. }
  43. module.exports.createTrigger = createTrigger;