Quartz.net - 每 m 个月在第 n 天重复?
Quartz.net - Repeat on day n, of every m months?
使用 Quartz.NET,我正在尝试创建一个触发器:
- 9 月 30 日开始,
- 在最后一天
重复
- 每 5 个月 。
- 能够使用
ITrigger.GetFireTimeAfter()
到 compute/project 下一次开火时间(UI 反馈)
预计:
- 2017-9-30
- 2018-2-28
- 2018-7-31
- 2018-12-31
我想我可以使用 CronTrigger
(即 0 0 0 L 9/5 ? *
)
但预计天数是:
- 2017-9-30
- 2018-9-30
- 2019-9-30
也无法使用 CalendarIntervalTrigger
:
For example, if you choose a start time that occurs on January 31st, and have a trigger with unit Month and interval 1, then the next fire time will be February 28th, and the next time after that will be March 28th - and essentially each subsequent firing will occur on the 28th of the month, even if a 31st day exists. If you want a trigger that always fires on the last day of the month - regardless of the number of days in the month, you should use ICronTrigger
如何安排这样的触发器?我必须从头开始实现自己的触发器和 IScheduler 吗?或者有没有办法让我注入自定义计算?
谢谢
这不是 cron 表达式的工作方式。通过将其定义为“9/5”,您只是表达了 "start at september" 和 "icrement by 5 month",但没有第十四个月。超过月数的每个月都将被削减,例如 1/5 将计算为 "January",然后是 "June",最后是 "November"。另一次尝试将溢出月数,cron 将从头开始。所以它将从另一个 "January" 开始,然后是 "June",最后是 "November"。看看这个表达式,如果它能像你想要的那样工作,它就不会适合明年的表达式,因为例如 1/5 将在 "April"
中计算
1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => April (2018) +5
这是错误的!因为四月不适合1/5
这是正确的行为:
1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => January (2018) +5
1/5 => June (2018)
...
您真正需要的是以不同方式工作的工具。我不认为有什么方法可以强制 Quartz.NET Cron 按你的意愿工作。唯一的方法是 "replace" Quartz.NET cron 评估器使用不同的东西。如果你四处看看,你会发现我是一个实现小型领域特定语言的作者的库,它应该更适合你想做的事情。我在这里描述了它:
使用 Quartz.NET,我正在尝试创建一个触发器:
- 9 月 30 日开始,
- 在最后一天 重复
- 每 5 个月 。
- 能够使用
ITrigger.GetFireTimeAfter()
到 compute/project 下一次开火时间(UI 反馈)
预计:
- 2017-9-30
- 2018-2-28
- 2018-7-31
- 2018-12-31
我想我可以使用 CronTrigger
(即 0 0 0 L 9/5 ? *
)
但预计天数是:
- 2017-9-30
- 2018-9-30
- 2019-9-30
也无法使用 CalendarIntervalTrigger
:
For example, if you choose a start time that occurs on January 31st, and have a trigger with unit Month and interval 1, then the next fire time will be February 28th, and the next time after that will be March 28th - and essentially each subsequent firing will occur on the 28th of the month, even if a 31st day exists. If you want a trigger that always fires on the last day of the month - regardless of the number of days in the month, you should use ICronTrigger
如何安排这样的触发器?我必须从头开始实现自己的触发器和 IScheduler 吗?或者有没有办法让我注入自定义计算?
谢谢
这不是 cron 表达式的工作方式。通过将其定义为“9/5”,您只是表达了 "start at september" 和 "icrement by 5 month",但没有第十四个月。超过月数的每个月都将被削减,例如 1/5 将计算为 "January",然后是 "June",最后是 "November"。另一次尝试将溢出月数,cron 将从头开始。所以它将从另一个 "January" 开始,然后是 "June",最后是 "November"。看看这个表达式,如果它能像你想要的那样工作,它就不会适合明年的表达式,因为例如 1/5 将在 "April"
中计算1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => April (2018) +5
这是错误的!因为四月不适合1/5
这是正确的行为:
1/5 => January (2017) +5
1/5 => June (2017) +5
1/5 => November (2017) +5
1/5 => January (2018) +5
1/5 => June (2018)
...
您真正需要的是以不同方式工作的工具。我不认为有什么方法可以强制 Quartz.NET Cron 按你的意愿工作。唯一的方法是 "replace" Quartz.NET cron 评估器使用不同的东西。如果你四处看看,你会发现我是一个实现小型领域特定语言的作者的库,它应该更适合你想做的事情。我在这里描述了它: