绝对不可能解释的结果为基于cron的调度器在Quartz [英] Absolutely unexplainable results for cron based scheduler in Quartz

查看:173
本文介绍了绝对不可能解释的结果为基于cron的调度器在Quartz的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a service class that responsible for scheduling of jobs based on user input. One of the methods of that class accepts objects with user input and builds cron expression for it. I started to create unit tests for each and every use-case and came across absolutely unexplainable discrepancy between two almost identical tests:

一个测试重复的情况工作每2天:

One tests the case of recurring job every 2 days:

"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
    }
    }


$ b

And it works every time without any issues.

另一个测试同样的事情,间隔4天:

And another, tests the same thing by with 4 days interval:

"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
    }

上一次工作几天前,然后我开始得到相同的错误,没有真正改变任何东西。

Last one used to work couple of days ago, and then I started to get the same error without really changing anything.


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

'Wed Jul 13 05:57:00 UTC 2016' is not equal to 'Mon Jul 11 05:57:00 UTC 2016'

这个错误意味着下一个计算日期不是第一个运行日期,而是后面的日期为什么会发生?

This error means that the next calculated date is not the first running date, but the one after. Why is it happening? What do I miss?

推荐答案

您似乎对cron表达式中如何解释1/4感到困惑。在day-of-month字段中,第1,5,9,13等等。所以,是的,如果现在是7月11日,下一场比赛将是7月13日。

You seem to be confused about how 1/4 is interpreted in a cron expression. In the day-of-month field, it will match days 1, 5, 9, 13, etc. So, yes, if "now" is July 11, the next match will be July 13.

这篇关于绝对不可能解释的结果为基于cron的调度器在Quartz的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆