Quartz.Net - 延迟一个简单的触发器启动

发布于 2024-09-14 14:10:38 字数 1390 浏览 3 评论 0原文

我在 Quartz 中设置了一些作业,以按设定的时间间隔运行。问题是,当服务启动时,它会尝试立即启动所有作业...有没有办法使用 .xml 配置为每个作业添加延迟?

以下是 2 个作业触发器示例:

 <simple>
    <name>ProductSaleInTrigger</name>
    <group>Jobs</group>
    <description>Triggers the ProductSaleIn job</description>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <volatile>false</volatile>
    <job-name>ProductSaleIn</job-name>
    <job-group>Jobs</job-group>
    <repeat-count>RepeatIndefinitely</repeat-count>
    <repeat-interval>86400000</repeat-interval>        
  </simple>

 <simple>
    <name>CustomersOutTrigger</name>
    <group>Jobs</group>
    <description>Triggers the CustomersOut job</description>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <volatile>false</volatile>
    <job-name>CustomersOut</job-name>
    <job-group>Jobs</job-group>
    <repeat-count>RepeatIndefinitely</repeat-count>
    <repeat-interval>43200000</repeat-interval> 
  </simple>

如您所见,有 2 个触发器,第一个触发器每天重复,下一个触发器每天重复两次。

我的问题是,我希望第一个或第二个作业在另一个作业几分钟后开始...(因为它们最终都访问相同的 API,并且我不想超载请求)

是否有重复延迟或优先属性?我找不到任何文档这么说..

I have a few jobs setup in Quartz to run at set intervals. The problem is though that when the service starts it tries to start all the jobs at once... is there a way to add a delay to each job using the .xml config?

Here are 2 job trigger examples:

 <simple>
    <name>ProductSaleInTrigger</name>
    <group>Jobs</group>
    <description>Triggers the ProductSaleIn job</description>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <volatile>false</volatile>
    <job-name>ProductSaleIn</job-name>
    <job-group>Jobs</job-group>
    <repeat-count>RepeatIndefinitely</repeat-count>
    <repeat-interval>86400000</repeat-interval>        
  </simple>

 <simple>
    <name>CustomersOutTrigger</name>
    <group>Jobs</group>
    <description>Triggers the CustomersOut job</description>
    <misfire-instruction>SmartPolicy</misfire-instruction>
    <volatile>false</volatile>
    <job-name>CustomersOut</job-name>
    <job-group>Jobs</job-group>
    <repeat-count>RepeatIndefinitely</repeat-count>
    <repeat-interval>43200000</repeat-interval> 
  </simple>

As you see there are 2 triggers, the first repeats every day, the next repeats twice a day.

My issue is that I want either the first or second job to start a few minutes after the other... (because they are both in the end, accessing the same API and I don't want to overload the request)

Is there a repeat-delay or priority property? I can't find any documentation saying so..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴意少年 2024-09-21 14:10:38

我知道您是通过 XML 执行此操作,但在代码中您可以将 StartTimeUtc 设置为延迟 30 秒,如下所示...

trigger.StartTimeUtc = DateTime.UtcNow.AddSeconds(30);

I know you are doing this via XML but in code you can set the StartTimeUtc to delay say 30 seconds like this...

trigger.StartTimeUtc = DateTime.UtcNow.AddSeconds(30);
小ぇ时光︴ 2024-09-21 14:10:38

对于您的 XML 文件来说,这并不是一个完美的答案 - 但通过代码,您可以在构建触发器时使用 StartAt 扩展方法。

/* calculate the next time you want your job to run - in this case top of the next hour */
var hourFromNow = DateTime.UtcNow.AddHours(1);
var topOfNextHour = new DateTime(hourFromNow.Year, hourFromNow.Month, hourFromNow.Day, hourFromNow.Hour, 0, 0);

/* build your trigger and call 'StartAt' */
TriggerBuilder.Create().WithIdentity("Delayed Job").WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever()).StartAt(new DateTimeOffset(topOfNextHour))

This isn't exactly a perfect answer for your XML file - but via code you can use the StartAt extension method when building your trigger.

/* calculate the next time you want your job to run - in this case top of the next hour */
var hourFromNow = DateTime.UtcNow.AddHours(1);
var topOfNextHour = new DateTime(hourFromNow.Year, hourFromNow.Month, hourFromNow.Day, hourFromNow.Hour, 0, 0);

/* build your trigger and call 'StartAt' */
TriggerBuilder.Create().WithIdentity("Delayed Job").WithSimpleSchedule(x => x.WithIntervalInSeconds(60).RepeatForever()).StartAt(new DateTimeOffset(topOfNextHour))
表情可笑 2024-09-21 14:10:38

您现在可能已经看到了这一点,但是可以链接作业,尽管不支持开箱即用。

http://quartznet.sourceforge.net/faq.html#howtochainjobs

You've probably already seen this by now, but it's possible to chain jobs, though it's not supported out of the box.

http://quartznet.sourceforge.net/faq.html#howtochainjobs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文