You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

91 lines
1.8 KiB

  1. module.exports = {
  2. cd: 1000,
  3. lastTime: null,
  4. init: function () {
  5. this.lastTime = this.getTime();
  6. },
  7. update: function () {
  8. this.lastTime = this.getTime();
  9. },
  10. shouldRun: function (c) {
  11. let cron = c.cron.split(' ');
  12. if (cron.length !== 5) {
  13. console.log('Invalid Cron Format: ' + cron.join(' '));
  14. return false;
  15. }
  16. let lastTime = this.lastTime;
  17. let time = this.getTime();
  18. let lastRun = c.lastRun;
  19. if (lastRun) {
  20. if (Object.keys(lastRun).every(e => (lastRun[e] === time[e])))
  21. return false;
  22. }
  23. let timeOverflows = [60, 24, 32, 12, 7];
  24. let run = ['minute', 'hour', 'day', 'month', 'weekday'].every(function (t, i) {
  25. let tCheck = cron[i];
  26. if (tCheck === '*')
  27. return true;
  28. let overflow = timeOverflows[i];
  29. let timeT = time[t];
  30. let lastTimeT = lastTime[t];
  31. if (timeT < lastTimeT)
  32. timeT += overflow;
  33. else if (timeT > lastTimeT)
  34. lastTimeT++;
  35. tCheck = tCheck.split(',');
  36. return Array
  37. .apply(null, Array(1 + timeT - lastTimeT))
  38. .map((i, j) => (j + lastTimeT))
  39. .some(function (s) {
  40. let useTime = (s >= overflow) ? (s - overflow) : s;
  41. return tCheck.some(function (f) {
  42. f = f.split('-');
  43. if (f.length === 1) {
  44. f = f[0].split('/');
  45. if (f.length === 1)
  46. return (useTime === f[0]);
  47. return ((useTime % f[1]) === 0);
  48. }
  49. return ((useTime >= f[0]) && (useTime <= f[1]));
  50. });
  51. });
  52. });
  53. if (run)
  54. c.lastRun = time;
  55. return run;
  56. },
  57. getTime: function () {
  58. let time = new Date();
  59. return {
  60. minute: time.getMinutes(),
  61. hour: time.getHours(),
  62. day: time.getDate(),
  63. month: time.getMonth(),
  64. weekday: time.getDay()
  65. };
  66. },
  67. daysInMonth: function (month) {
  68. let year = (new Date()).getYear();
  69. return new Date(year, month, 0).getDate();
  70. }
  71. };