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

28,769

Solution 1

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.

Solution 2

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();
Share:
28,769
Bittercoder
Author by

Bittercoder

Alex Henderson - Managing Director at DevDefined Limited, a boutique software development firm in Auckland, New Zealand - specializing in ALT.Net, end-to-end Product Development and assisting startups. "Self Confessed Coding Junky for 15 years"

Updated on April 14, 2020

Comments

  • Bittercoder
    Bittercoder about 4 years

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

  • dmigo
    dmigo over 9 years
    Won't there be any problem with Daylight Svaing Time? (see here)
  • Nguyễn Huy
    Nguyễn Huy about 9 years
    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?
  • sgmoore
    sgmoore about 9 years
    @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();
  • Nguyễn Huy
    Nguyễn Huy about 9 years
    @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.
  • Admin
    Admin almost 9 years
    nice ... this also gets around the DST problem in a really elegant way!
  • Jürgen Steinblock
    Jürgen Steinblock almost 9 years
    @AndreasNiedermair Glad I could help, just for my personal knowlege: Does quartz.net's cron like schedule have any dst issues?
  • Admin
    Admin almost 9 years
    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)
  • Mou
    Mou over 7 years
    what is the meaning of EndingDailyAfterCount ?
  • Jürgen Steinblock
    Jürgen Steinblock over 7 years
    @Mou 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)
  • Worthy7
    Worthy7 over 7 years
    Just sent 14600 emails to myself dammit.