1

I have Jobs with multiple Triggers. In some cases, the Triggers will overlap. When Quartz.NET determines the overlapping Triggers need to be triggered, the IJob.Execute(IJobExecutionContext context) method is executed for each Trigger.

Is there a way to prevent this overlap?

Thanks in advance.

Rob G
  • 25
  • 4
  • 1
    Possible duplicate of [What is exactly mean by 'DisallowConcurrentExecution' in Quartz.net](https://stackoverflow.com/questions/23390698/what-is-exactly-mean-by-disallowconcurrentexecution-in-quartz-net) – Najera Nov 21 '17 at 15:36
  • Just use `[DisallowConcurrentExecution]` attribute in the job implementation – Najera Nov 21 '17 at 15:37
  • @Najera Thanks for the response. I have tried this, but all this does is prevent the triggered instances of the job running simultaneously. If I have three triggers that all trigger at the same time, the attribute you mentioned forces the job to run three times sequentially. – Rob G Nov 21 '17 at 16:07
  • That is a different need, how often this needs to run once? – Najera Nov 21 '17 at 16:38
  • It will always need to run once; regardless of how many triggers fire for that job. – Rob G Nov 21 '17 at 16:41
  • Triggers are fired exactly same time? – Najera Nov 21 '17 at 16:42
  • They are, indeed. – Rob G Nov 21 '17 at 16:53

1 Answers1

1

I have not tested this, but I would give it a chance:

[DisallowConcurrentExecution]
public class Job : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        bool wasExecutedAt = WasExecutedAt(context.ScheduledFireTimeUtc);
    }

    private bool WasExecutedAt(DateTimeOffset? fireTime)
    {
         // Check and maintain the state
    }
}
Najera
  • 2,869
  • 3
  • 28
  • 52
  • This appears to have worked. Thanks Najera! For others who see this post, I persisted in the "context.JobDetail.JobDataMap" object, and added the [PersistJobDataAfterExecution] attribute against the class. – Rob G Nov 23 '17 at 16:24
  • In theory, @Najera, your code is fine. Unfortunately, there is a bug in Quartz.NET (appears to be going on since at least 2016) whereby the "ScheduledFireTimeUtc" is actually set to the "FireTimeUtc". Do you happen to know if there is a way to get the proper value for the schedule fire time, at the time it actually triggered? The bug reported on github for this is: https://github.com/quartznet/quartznet/issues/605 – Rob G Mar 14 '18 at 10:18
  • I refactored to create a base class implementing IJob, and I put the DisallowConcurrentExecution and PersistJobDataAfterExecution attributes onto the base class. I had to move the attributes and put them against all of my derived classes to make it all start working again. This is a shame, as the code in my base class (based on @Najera example) is useless without those attributes. – Rob G Mar 16 '18 at 14:25