【问题标题】:Absolutely unexplainable results for cron based scheduler in QuartzQuartz 中基于 cron 的调度程序的绝对无法解释的结果
【发布时间】:2016-11-13 00:01:45
【问题描述】:

我们有一个服务类,负责根据用户输入安排作业。该类的方法之一接受具有用户输入的对象并为其构建 cron 表达式。 我开始为每个用例创建单元测试,并且在两个几乎相同的测试之间遇到了绝对无法解释的差异:

每 2 天测试一次重复作​​业的情况:

"ChecklistCreationScheduler#buildCronExpression" should {
    "build correct cron expressions for day interval of 2" in {
      val jobScheduler = mock[JobScheduler]
      val futureChecklistRepository = mock[FutureChecklistRepository]

      val chlCreationScheduler = new ChecklistCreationScheduler(jobScheduler, futureChecklistRepository)

      val now = DateTime.now.withSecondOfMinute(0).withMillisOfSecond(0)

      val dayIntervalForm = mock[CreateJobForm]
      dayIntervalForm.maybeDayInterval returns Some(2)

      val cronStr = chlCreationScheduler.buildCronExpression(List(dayIntervalForm), now)
      cronStr must_== "0 %s %s 1/2 * ? *".format(now.getMinuteOfHour, now.getHourOfDay)

      val cronExpression = new CronExpression(cronStr)
      val firstRun = cronExpression.getNextValidTimeAfter(now.minusMinutes(1).toDate)
      firstRun must_== now.toDate
      cronExpression.getNextValidTimeAfter(firstRun) must_== now.plusDays(2).toDate
    }
    }

而且它每次都能正常工作。

另外,每隔 4 天测试同样的东西:

"build correct cron expressions for day interval of 4" in {
      val jobScheduler = mock[JobScheduler]
      val futureChecklistRepository = mock[FutureChecklistRepository]

      val chlCreationScheduler = new ChecklistCreationScheduler(jobScheduler, futureChecklistRepository)

      val now = DateTime.now.withSecondOfMinute(0).withMillisOfSecond(0)

      val dayIntervalForm = mock[CreateJobForm]
      dayIntervalForm.maybeDayInterval returns Some(4)

      val cronStr = chlCreationScheduler.buildCronExpression(List(dayIntervalForm), now)
      cronStr must_== "0 %s %s 1/4 * ? *".format(now.getMinuteOfHour, now.getHourOfDay)

      val cronExpression = new CronExpression(cronStr)
      val firstRun = cronExpression.getNextValidTimeAfter(now.minusMinutes(1).toDate)
      firstRun must_== now.toDate
      cronExpression.getNextValidTimeAfter(firstRun) must_== now.plusDays(4).toDate
    }

最后一个曾经在几天前工作,然后我开始遇到同样的错误而没有真正改变任何东西。

'Wed Jul 13 05:57:00 UTC 2016' 不等于'Mon Jul 11​​ 05:57:00 UTC 2016'

这个错误意味着下一个计算的日期不是第一个运行日期,而是后面的那个。为什么会这样?我错过了什么?

【问题讨论】:

    标签: scala cron quartz-scheduler scheduling


    【解决方案1】:

    您似乎对如何在 cron 表达式中解释 1/4 感到困惑。在 day-of-month 字段中,它将匹配第 1、5、9、13 天等。所以,是的,如果“现在”是 7 月 11 日,那么下一个匹配将是 7 月 13 日。

    【讨论】:

    • 那么,这是否意味着我还需要确定该表达式的开始日期,才能使其正常工作?
    • 这真的取决于你想要做什么。如果您想要一个每 n 天一次的时间表,那么CalendarInterval(参见CalendarIntervalScheduleBuilder.withIntervalInDays)比CronExpression 更合适。
    猜你喜欢
    • 2014-09-28
    • 2023-03-04
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多