dmose
dmose

Reputation: 393

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

Upvotes: 6

Views: 10088

Answers (3)

Joseph
Joseph

Reputation: 361

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

Upvotes: 2

Michael Kobaly
Michael Kobaly

Reputation: 135

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

Upvotes: 4

Chris Rock
Chris Rock

Reputation: 71

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

Upvotes: 0

Related Questions