Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a paused trigger

Tags:

c#

quartz.net

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);
like image 306
Ben Foster Avatar asked Aug 21 '13 17:08

Ben Foster


2 Answers

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);
}
like image 192
nollidge Avatar answered Sep 19 '22 18:09

nollidge


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);
like image 42
LeftyX Avatar answered Sep 20 '22 18:09

LeftyX