Quartz.Net - delay a simple trigger to start

穿精又带淫゛_ 提交于 2019-12-10 15:45:52

问题


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..


回答1:


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);



回答2:


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))



回答3:


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



来源:https://stackoverflow.com/questions/3515542/quartz-net-delay-a-simple-trigger-to-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!