實際上可以用觸發組來做到這一點。您只需暫停特定的觸發組,然後創建其觸發器屬於該組的作業。
// pauses the group, if it isn't already
scheduler.PauseTriggers(GroupMatcher<TriggerKey>.GroupEquals("PausedGroup"));
var job = JobBuilder.Create<MyJob>().WithIdentity(jobName).Build();
var trigger = TriggerBuilder.Create()
.ForJob(job)
.WithIdentity(Guid.NewGuid().ToString(), "PausedGroup")
.WithSimpleSchedule(schedule => schedule.WithIntervalInMinutes(1).RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);
然後恢復工作,重新安排每個觸發器具有不同的組:
foreach (var oldTrigger in scheduler.GetTriggers(new JobKey(jobName)))
{
// clone the old trigger with new group name
var newTrigger = oldTrigger.GetTriggerBuilder()
.WithIdentity(oldTrigger.Key.Name) // uses default trigger group name
.Build();
// not a typo, the method to reschedule a trigger is called "RescheduleJob" for some reason
scheduler.RescheduleJob(oldTrigger.Key, newTrigger);
}
它實際上是可能的觸發羣體 - 檢查我的答案。 – nollidge
@nollidge:謝謝你的澄清。雖然這個答案是4歲。 – LeftyX