Quartz.Net how to create a daily schedule that does not gain 1 minute per day
Asked Answered
M

2

41

I am trying to build a repeating daily schedule in Quartz.Net but having a few issues:

First off, I build a daily schedule, repating at 12:45 Using Quartz.Net code like this:

var trigger = TriggerBuilder.Create()
  .WithDailyTimeIntervalSchedule(s => 
      s.OnEveryDay().StartingDailyAt(new TimeOfDay(13, 00)))
.Build();

var times = TriggerUtils.ComputeFireTimes(trigger as IOperableTrigger, null, 10);

foreach (var time in times) Console.WriteLine(time);

This is being executed in New Zealand, DST (so UTC+13:00)

And the output I get is rather strange:

5/10/2012 1:00:00 p.m. +13:00
5/10/2012 12:01:00 a.m. +00:00
5/10/2012 12:02:00 a.m. +00:00
5/10/2012 12:03:00 a.m. +00:00
5/10/2012 12:04:00 a.m. +00:00
5/10/2012 12:05:00 a.m. +00:00
5/10/2012 12:06:00 a.m. +00:00
5/10/2012 12:07:00 a.m. +00:00
5/10/2012 12:08:00 a.m. +00:00
5/10/2012 12:09:00 a.m. +00:00

The first date/time is displayed using local timezone, then the rest are displayed with UTC, and each time value is incremented by 1 minute, and the date never changes.

I feel like I might be missing something fundamental here with the daily time interval schedule, but I just don't know what it is?

Edit

As an update to do this, I have now switched to using a CRON expression based trigger:

TriggerBuilder.Create()
  .WithCronSchedule(string.Format("0 {0} {1} ? * *", 0, 13))
  .Build();

And it gave me the results I would expect:

5/10/2012 12:00:00 a.m. +00:00
6/10/2012 12:00:00 a.m. +00:00
7/10/2012 12:00:00 a.m. +00:00
8/10/2012 12:00:00 a.m. +00:00
9/10/2012 12:00:00 a.m. +00:00
10/10/2012 12:00:00 a.m. +00:00
11/10/2012 12:00:00 a.m. +00:00
12/10/2012 12:00:00 a.m. +00:00
13/10/2012 12:00:00 a.m. +00:00
14/10/2012 12:00:00 a.m. +00:00

But I would still like to know why the DailyTimeIntervale schedule is not working...

Makeyevka answered 4/10, 2012 at 20:53 Comment(1)
TriggerUtils.ComputeFireTimes -- awesome!Poona
T
57

You aren't specifying the interval which happens to default to 1 minute, so it assumes you want to run the job every minute.

Try

 ITrigger trigger = TriggerBuilder.Create()
    .WithDailyTimeIntervalSchedule
      (s => 
         s.WithIntervalInHours(24)
        .OnEveryDay()
        .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(13, 0))
      )
    .Build();

The default should be to run every day, so the OnEveryDay() isn't really needed.

Not sure why you are seeing local and UTC, as all my times are displayed in UTC.

Tamarisk answered 8/10, 2012 at 17:46 Comment(5)
Won't there be any problem with Daylight Svaing Time? (see here)Stadler
i have more question for this, if i want to start daily and two times at 12 AM and 12 PM, how i do that?Endostosis
@NguyễnHuy. I guess ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(12)) .Build(); or ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(12) .OnEveryDay() .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0)) ) .Build();Tamarisk
@sgmoore: i had given a time for examble, so maybe you misunderstand my mean, my mean is how to set multiple time in daily. For ex: 2am;3am;5am. These times have not the same period of time.Endostosis
Just sent 14600 emails to myself dammit.Declivity
H
25

While WithIntervalInHours will probably solve this and a cron like schedule is even more flexible, I want to share another solution: EndingDailyAfterCount(...)

var trigger = TriggerBuilder.Create()
  .WithDailyTimeIntervalSchedule(s => s
      .OnEveryDay()
      .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(13, 00))
      .EndingDailyAfterCount(1))
.Build();
Hindsight answered 14/4, 2015 at 9:28 Comment(5)
nice ... this also gets around the DST problem in a really elegant way!Platto
@AndreasNiedermair Glad I could help, just for my personal knowlege: Does quartz.net's cron like schedule have any dst issues?Paternity
Ah, as far as I can remember: internal time is stored in UTC, so completely agnostic to DST - probably you'd have to reschedule jobs after DST change to reflect change in the associated UTC timestamp... (But I don't know to which extend - futurewise - triggers get added when adding a cron expression, or if they get evaluated on the fly)Platto
what is the meaning of EndingDailyAfterCount ?Ackley
@Ackley This will stop the schedule per day after x runs. If you want to schedule a job to run every hour from 8 AM to 8 PM you can use OnEveryDay().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(8, 0)).WithIntervalInHours(1).EndingDailyAfter(12)Paternity

© 2022 - 2024 — McMap. All rights reserved.