0

I've written the rescheduling code as shown below. Despite the schedule saying "repeat every 120 seconds", the Execute() method keeps getting called immediately after concluding the UpdateQuartzJobTrigger method.

 class Model : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Debug.WriteLine("Executing Job: " + DateTime.Now.ToShortTimeString());
            //Do some stuff
            UpdateQuartzJobTrigger(context);
        }
        private void UpdateQuartzJobTrigger( IJobExecutionContext context )
        {
            // Trigger the job to run now, and then every 120 seconds
            ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity(context.Trigger.Key.Name, context.Trigger.Key.Group)
              .WithSimpleSchedule(x => x
                  .WithIntervalInSeconds(120)
                  .RepeatForever())
              .Build();
            var result = context.Scheduler.RescheduleJob(context.Trigger.Key, trigger);
        }
    }
Ashish
  • 65
  • 5
  • Possible duplicate of [Quartz.Net - delay a simple trigger to start](http://stackoverflow.com/questions/3515542/quartz-net-delay-a-simple-trigger-to-start) – Andrew Morton Apr 03 '16 at 18:07
  • @AndrewMorton, these two are far from duplicates. The OP in your question is trying to delay triggers when **starting** a service. I'm not trying to delay anything, I want the the trigger to get rescheduled and fire when the criteria for that schedule is met. – Ashish Apr 03 '16 at 19:34

2 Answers2

1

IMHO, a best practice would be to not combine "Execute" and then RescheduleJob.

The WithIntervalInSeconds will start "immediately"...wait X seconds, and then go again. It does not "delay" for X seconds.

So basically, you are completing your "job", then IMMEDIATELY rescheduling it...but since there is no delay (as @AM points out), it gets rescheduled immediately.

I would separate your job-creation logic from your job-execution logic.

Or look here:

How to do “sequential” Job Scheduling (Quartz?)

But what you are experiencing is correct, as-designed behavior. WithIntervalInSeconds does not have a "delay the first execution" behavior. It is the time between the 1st and 2nd executions....(and the 2nd and 3rd and so on and so on).

Community
  • 1
  • 1
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Thanks @granadacoder. I find this behavior to be a bit odd, because there is another method in the TriggerBuilder called StartNow(), which now seems redundant if WithInterval is used. – Ashish Apr 04 '16 at 16:15
  • I blame the java guys (since its a java port over) for all the quirkies!! (haha) – granadaCoder Apr 04 '16 at 16:17
1

The comment in your code even indicates that it supposed to // Trigger the job to run now, and then every 120 seconds

If you don't want it running now, then set a start time, eg

        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity(context.Trigger.Key.Name, context.Trigger.Key.Group)
          .WithSimpleSchedule(x => x
              .WithIntervalInSeconds(120)
              .RepeatForever())
          .StartAt(DateBuilder.FutureDate(120, IntervalUnit.Second))   
          .Build();
sgmoore
  • 15,694
  • 5
  • 43
  • 67