Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

119 linhas
2.4 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. isActive: function (c) {
  11. let cron = c.cron.split(' ');
  12. if (cron.length !== 5)
  13. return false;
  14. let time = this.getTime();
  15. return ['minute', 'hour', 'day', 'month', 'weekday'].every((t, i) => {
  16. let f = cron[i].split('-');
  17. if (f[0] === '*')
  18. return true;
  19. let useTime = time[t];
  20. if (f.length === 1) {
  21. f = f[0].split('/');
  22. if (f.length === 1) {
  23. const options = f[0].split(',');
  24. const isOk = options.some(o => {
  25. return ~~o === useTime;
  26. });
  27. return isOk;
  28. }
  29. return ((useTime % f[1]) === 0);
  30. }
  31. return ((useTime >= f[0]) && (useTime <= f[1]));
  32. });
  33. },
  34. shouldRun: function (c) {
  35. let cron = c.cron.split(' ');
  36. if (cron.length !== 5)
  37. return false;
  38. let lastTime = this.lastTime;
  39. let time = this.getTime();
  40. let lastRun = c.lastRun;
  41. if (lastRun) {
  42. if (Object.keys(lastRun).every(e => (lastRun[e] === time[e])))
  43. return false;
  44. }
  45. let timeOverflows = [60, 24, 32, 12, 7];
  46. let run = ['minute', 'hour', 'day', 'month', 'weekday'].every(function (t, i) {
  47. let tCheck = cron[i];
  48. if (tCheck === '*')
  49. return true;
  50. let overflow = timeOverflows[i];
  51. let timeT = time[t];
  52. let lastTimeT = lastTime[t];
  53. if (timeT < lastTimeT)
  54. timeT += overflow;
  55. else if (timeT > lastTimeT)
  56. lastTimeT++;
  57. tCheck = tCheck.split(',');
  58. return Array
  59. .apply(null, Array(1 + timeT - lastTimeT))
  60. .map((j, k) => (k + lastTimeT))
  61. .some(function (s) {
  62. let useTime = (s >= overflow) ? (s - overflow) : s;
  63. return tCheck.some(function (f) {
  64. f = f.split('-');
  65. if (f.length === 1) {
  66. f = f[0].split('/');
  67. if (f.length === 1)
  68. return (useTime === ~~f[0]);
  69. return ((useTime % f[1]) === 0);
  70. }
  71. return ((useTime >= f[0]) && (useTime <= f[1]));
  72. });
  73. });
  74. });
  75. if (run)
  76. c.lastRun = time;
  77. return run;
  78. },
  79. getTime: function () {
  80. let time = new Date();
  81. return {
  82. minute: time.getMinutes(),
  83. hour: time.getHours(),
  84. day: time.getDate(),
  85. month: time.getMonth() + 1,
  86. year: time.getUTCFullYear(),
  87. weekday: time.getDay()
  88. };
  89. },
  90. daysInMonth: function (month) {
  91. let year = (new Date()).getYear();
  92. return new Date(year, month, 0).getDate();
  93. }
  94. };