Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.Net - delay a simple trigger to start

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

like image 516
dmose Avatar asked Aug 18 '10 18:08

dmose


2 Answers

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);
like image 88
Michael Kobaly Avatar answered Sep 17 '22 07:09

Michael Kobaly


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))
like image 41
Joseph Avatar answered Sep 21 '22 07:09

Joseph