scheduleTest.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var schedule = require('../lib/schedule');
  2. var cronTrigger = require('../lib/cronTrigger');
  3. var jobMap = [];
  4. var simpleJob = function(data){
  5. var t = data.id+data.period;
  6. console.log("run for simple job :" + data.id + " period: " + data.period + " at time " + (new Date()));
  7. }
  8. var cronJob = function(data){
  9. console.log("run for cronJob :" + data.id + " at time " + (new Date()));
  10. }
  11. function scheduleSimpleJobTest(count){
  12. var id = 0;
  13. for(var i = 0; i < count; i ++){
  14. var time = Math.ceil(Math.random() * 10 * 1000);
  15. var period = Math.ceil(Math.random()*60 * 1000 + 100)
  16. var id = schedule.scheduleJob({start:Date.now()+time,period:period}, simpleJob, {id:id++, period:period});
  17. jobMap.push(id);
  18. }
  19. }
  20. function scheduleCronJobTest(count){
  21. var id = 0;
  22. // var trigger = cronTrigger.decodeTrigger('* * 2-20 * * *');
  23. for(var i = 0; i < count; i++){
  24. var second = Math.floor(Math.random()*10);
  25. var id = schedule.scheduleJob('0/2,2-10,13-20,40-45,55-56 * * * * *', cronJob, {id:id++});
  26. jobMap.push(id);
  27. }
  28. }
  29. function cancleJob(data){
  30. var jobMap = data.jobMap;
  31. if(jobMap.length>0){
  32. var id = jobMap.pop();
  33. console.log("Cancel job : " + id + " Last jobs count : " + jobMap.length);
  34. data.schedule.cancelJob(id);
  35. }else
  36. console.log("All job has been cancled");
  37. }
  38. function scheduleCancleJobTest(){
  39. var id = schedule.scheduleJob({start:Date.now(),period:100, count:jobMap.length}, cancleJob, {jobMap:jobMap,schedule:schedule});
  40. }
  41. function test(){
  42. scheduleSimpleJobTest(5);
  43. scheduleCronJobTest(5);
  44. // scheduleCancleJobTest();
  45. }
  46. test();
  47. //schedule.scheduleJob({period:30, count:4}, simpleJob, {name:'simpleJob'});