Question

I have a cron job that Runs using Quartz.NET.

    <cron-expression>0 5 * * * ?</cron-expression>

It runs every 5 minutes as you can see above, but I want it to not run from 5pm on 30/Dec/2013 to 7am on 01/Jan/2014..

Is this possible does anyone know? Thanks Neil

Was it helpful?

Solution

Exclusion are most easily made with calendars. With calendar you can exclude a set of days, weekdays, time of days or a cron expression from schedule. Calendar is checked always checked after fire time has been determined (is this OK to run?).

You could either chain multiple calendars because you have nasty complexity of passing the year, multiple days and time range on top of that. More readable and understandable solution could be to implement your own fixed time range calendar (implementing ICalendar) that takes end and start time and excludes them.

You could naturally also make your first cron trigger end at 2013-12-30 17:00 and add a new trigger that starts at 2014-01-01 07:00.

Here's a trivial sample utilizing CronCalendar:

var calendar = new CronCalendar("0 * 1 * * ?");
scheduler.AddCalendar("myCalendar", calendar, replace: false, updateTriggers: false);

var trigger = TriggerBuilder.Create()
    .WithCronSchedule("0 5 * * * ?")
    .ModifiedByCalendar("myCalendar")
    .Build();

scheduler.ScheduleJob(trigger);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top