2

What is the best way to tackle setting up a job to run every hour of every day except for Monday morning at 3am? My ideal goal is to have an alternate job run in that position.

I have my two classes setup to be the Jobs - UpdateJob and FullJob. FullJob is the one that should only work once per week.

walen
  • 7,103
  • 2
  • 37
  • 58
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • Have you tried [`CronTrigger`](http://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/crontriggers.html)? – admdrew Sep 11 '14 at 18:40
  • I'm looking at it. I can figure out the first part. Just no idea how to exclude the 3am in the first Trigger. – BuddyJoe Sep 11 '14 at 18:51
  • Can you do 2 triggers? One would be "every hour on every day, except mondays" (`* * * * SUN,TUE,WED,THU,FRI,SAT`), the other "every hour, except 3am, on mondays" (`* 0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * MON`) – admdrew Sep 11 '14 at 18:58
  • 1
    That could work and is a nice approach. Can you make that an official answer. thanks. – BuddyJoe Sep 11 '14 at 21:09

2 Answers2

3

From an older cron-related question/answer, I received the idea to use two CronTriggers to accomplish this:

  1. "every hour, on every day, except Mondays":

    * * * * SUN,TUE,WED,THU,FRI,SAT
    
  2. "every hour, except 3am, on Mondays":

    * 0,1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * MON
    
Community
  • 1
  • 1
admdrew
  • 3,790
  • 4
  • 27
  • 39
0

I had slightly different requirements. However, I thought I'd leave this here for future readers.

Basically you can set advanced scheduling with a CronTrigger which takes a CronExpression.

Lesson 6: CronTrigger

CronTriggers are often more useful than SimpleTrigger, if you need a job-firing schedule that recurs based on calendar-like notions, rather than on the exactly specified intervals of SimpleTrigger.

With CronTrigger, you can specify firing-schedules such as “every Friday at noon”, or “every weekday and 9:30 am”, or even “every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday”.

Even so, like SimpleTrigger, CronTrigger has a startTime which specifies when the schedule is in force, and an (optional) endTime that specifies when the schedule should be discontinued.

Cron Expressions

Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:

  • Seconds
  • Minutes
  • Hours
  • Day-of-Month
  • Month
  • Day-of-Week
  • Year (optional field)

An example of a complete cron-expression is the string “0 0 12 ? * WED” - which means “every Wednesday at 12:00 pm”.

Example Implementation

There are many ways to set and scaffold Quartz.Net, and the code I use may vary a lot to other implementations, however someone might find this useful.

public static void SetSchedule<T>(this IScheduler source, TimeSpan minWaitSeconds, string cron)
   where T : IJob
{
   var jobName = typeof(T).Name;

   var triggerKey = new TriggerKey($"{jobName} Trigger");

   DateTimeOffset minNextTime = DateTime.UtcNow.AddSeconds(2) + minWaitSeconds;

   var trigger = TriggerBuilder.Create()
                               .WithIdentity(triggerKey)
                               .StartAt(minNextTime)
                               .WithCronSchedule(cron)
                               .Build();
       
   var jobKey = new JobKey(jobName);

   var job = JobBuilder.Create<T>()
                       .WithIdentity(jobKey)
                       .Build();

   source.ScheduleJob(job, trigger);
}

Usage

var startAt = new TimeSpan(0, 0, 0, 5);
var schedule = "0/5 * 8-16 ? * MON-FRI"

scheduler.SetSchedule<WarehousePickupNotificationJob>(startAt, schedule);

Note : My use case was that I needed a Task to run every 5 seconds, between certain hours, on week days.

Breakdown

"0/5 * 8-17 ? * MON-FRI"
  • 0/5
  • Seconds (The / character can be used to specify increments to values), which reads as run every 5 seconds
  • *
  • Minutes (* denotes all), which reads all minutes
  • 8-16
  • Hours (- denotes to), which reads from "8am to 5pm" Not that the end number is inclusive
  • ?
  • Day-of-Month (The ? character is allowed for the day-of-month and day-of-week fields. It is used to specify "no specific value")
  • *
  • Month field (* denotes all), which reads all months
  • MON-FRI
  • Day-of-Week (- denotes to), which reads all week days
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141