Is it possible to add a trigger to the scheduler in a paused state? I've found that if I don't specify a start time it will start immediately.
Other than immediately calling scheduler.PauseJob
can this be done another way?
var trigger = TriggerBuilder.Create()
.ForJob(job)
.WithSimpleSchedule(schedule => schedule.WithIntervalInMinutes(1).RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);
// Pause job immediately
scheduler.PauseJob(job.Key);
It is actually possible to do this with trigger groups. You just need to pause a particular trigger group, and then create a job whose trigger(s) belong to that group.
// 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);
Then to resume the job, reschedule each trigger with a different group:
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);
}
I've checked the repository and it does not seem to be possible.
You cannot pause a trigger/job until it has been scheduled.
Scheduler.PauseTrigger
and Scheduler.PauseJob
set the state of the trigger to InternalTriggerState.Paused
(see reference)
I would suggest you to schedule your trigger delaying the start a little bit and pause it immediately.
var trigger = TriggerBuilder.Create()
.ForJob(job)
.WithSimpleSchedule(schedule => schedule.WithIntervalInMinutes(1).RepeatForever())
.StartAt(SystemTime.UtcNow().AddSeconds(5))
.Build();
scheduler.ScheduleJob(job, trigger);
scheduler.PauseTrigger(trigger.Key);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With