Reputation: 17915
I need to run job on 2 different schedules (morning and afternoon). I know how to run it on one schedule, but not sure how to set it so 2 schedules trigger this job
var saferWatchJobDetail = JobBuilder.Create<SaferWatchProcessor>().Build();
var swtriggerMorning = TriggerBuilder.Create().WithCronSchedule("0 10 6 ? * MON-SUN *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
var swtriggerAfternoon = TriggerBuilder.Create().WithCronSchedule("0 10 13 ? * MON-FRI *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
this.scheduler.ScheduleJob(saferWatchJobDetail, swtriggerMorning);
According to documentation it seems like I need to use ScheduleJob
override with ISet<ITrigger>
but I'm not sure about 2 things:
ISet
and how do I instantiate this object?replace
? Not sure how should I use it. And what is the "key"? From documentation:If any of the given job or triggers already exist (or more specifically, if the keys are not unique) and the replace parameter is not set to true then an exception will be thrown.
Upvotes: 1
Views: 4107
Reputation: 4129
granaCoder's answer didn't work for me at all. It kept throwing exceptions. Not sure why.
If you want to use triggerset, which ended up working for me, this is how you do it:
var triggerSet = new Quartz.Collection.HashSet<ITrigger>();
var saferWatchJobDetail = JobBuilder.Create<SaferWatchProcessor>().Build();
var swtriggerMorning = TriggerBuilder.Create().WithCronSchedule("0 10 6 ? * MON-SUN *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
var swtriggerAfternoon = TriggerBuilder.Create().WithCronSchedule("0 10 13 ? * MON-FRI *", cs => cs.InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"))).Build();
triggerSet.Add(swtriggerMorning);
triggerSet.Add(swtriggerAfternoon);
this.scheduler.ScheduleJob(saferWatchJobDetail, triggerSet, true);
Upvotes: 4