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

Steven V
  • 16,357
  • 3
  • 63
  • 76
Bittercoder
  • 11,753
  • 10
  • 58
  • 76

2 Answers2

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.

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • 2
    Won't there be any problem with Daylight Svaing Time? (see [here](http://www.timeanddate.com/time/change/usa/los-angeles)) – dmigo Nov 24 '14 at 19:24
  • 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? – Nguyễn Huy Apr 07 '15 at 06:18
  • @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();` – sgmoore Apr 08 '15 at 13:30
  • @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. – Nguyễn Huy Apr 09 '15 at 02:25
  • 5
    Just sent 14600 emails to myself dammit. – Worthy7 Mar 17 '17 at 01:02
23

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();
Rafi Henig
  • 5,950
  • 2
  • 16
  • 36
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
  • 1
    nice ... this also gets around the DST problem in a really elegant way! –  Jul 15 '15 at 14:52
  • @AndreasNiedermair Glad I could help, just for my personal knowlege: Does quartz.net's cron like schedule have any dst issues? – Jürgen Steinblock Jul 16 '15 at 06:20
  • 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) –  Jul 16 '15 at 07:34
  • what is the meaning of `EndingDailyAfterCount` ? – Mou Feb 20 '17 at 13:14
  • 1
    @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)` – Jürgen Steinblock Feb 21 '17 at 07:37