shikeying
2024-01-19 54ea586ece304ef2569a345c9b26b2a9b9702c8a
walker-scheduler/src/main/java/com/walker/scheduler/Option.java
@@ -56,6 +56,18 @@
   private int currentTaskMonth = 0;         // 周期性,当前执行到的月,记录
   private int currentTaskYear = 0;            // 周期性,当前执行到的年,记录
   public int getCurrentTaskDay() {
      return currentTaskDay;
   }
   public int getCurrentTaskMonth() {
      return currentTaskMonth;
   }
   public int getCurrentTaskYear() {
      return currentTaskYear;
   }
   public String getTimeRangesValue() {
      StringBuilder s = new StringBuilder();
      for(Integer[] vals : timeRanges){
@@ -110,6 +122,43 @@
      this.currentTaskMonth = month;
      this.currentTaskDay = day;
//      this.currentTaskHour = hour;
      // 2024-01-18,修复:如果已经过了今天时间点的任务,如:凌晨2点,则需要自动切换到下一个时间。
      // 否则出现任务永远无法执行情况。
      if(this.isCycleTask){
         Option.TimeObject timeObj = this.isAvailableTime(System.currentTimeMillis());
         int currentHour = DateUtils.getCurrentHour();
         int currentDay = DateUtils.getCurrentYearMonthDay()[2];
         if(this.periodType == PeriodType.DAY){
            if(currentHour > hour){
               // 今天已经过了时间点,自动切换到下一次
               this.scheduleToNext(timeObj);
               logger.info("今天已经过了时间点,自动切换到下一次,option={}", this);
            }
         } else if (this.periodType == PeriodType.MONTH) {
            if(currentDay > day){
               this.scheduleToNext(timeObj);
               logger.info("本月当前日期{}已超过设置日期{},自动切换到下一次,option={}", currentDay, day, this);
            } else if(currentDay == day && (currentHour > hour)){
               this.scheduleToNext(timeObj);
               logger.info("本月日期已到达,但由于时间已过期,因此任务会在下个月执行,option={}", this);
            }
         } else if (this.periodType == PeriodType.YEAR) {
            int currentMonth = DateUtils.getCurrentYearMonthDay()[1];
            if(currentMonth > month){
               this.scheduleToNext(timeObj);
               logger.info("本年当前月份{}已超过设置月份{},自动切换到下一次,option={}", currentMonth, month, this);
            } else if (currentMonth == month && (currentDay > day)) {
               this.scheduleToNext(timeObj);
               logger.info("本年当前月份相同,但当前日期{}已超过设置日期{},自动切换到下一次,option={}", currentDay, day, this);
            } else if(currentMonth == month && (currentDay == day) && currentHour > hour){
               this.scheduleToNext(timeObj);
               logger.info("本年当前月份日期相同,但当前时间{}已超过设置时间{},自动切换到下一次,option={}", currentHour, hour, this);
            }
         }
      }
   }
   /**
@@ -454,6 +503,17 @@
      }
   }
   @Override
   public String toString(){
      return new StringBuilder("[periodType=").append(this.periodType)
            .append(", timeType=").append(this.timeType)
            .append(", timeRanges=").append(this.timeRanges)
            .append(", isCycleTask=").append(this.isCycleTask)
            .append(", setYearMonthDayHour=").append(this.year).append("-").append(this.month).append("-").append(this.day).append("-").append(this.hour)
            .append(", nextTime=").append(this.currentTaskYear).append("-").append(this.currentTaskMonth).append("-").append(this.currentTaskDay)
            .append("]").toString();
   }
   public static void main(String[] args){
      /**
      long test = System.currentTimeMillis();