18

I'm building a windows service which is performing a scheduled task which processes a queue of commands (from a legacy system) at a regular interval (once per min) using Quartz.net

If the task takes longer than 1 min, which would be unusual but possible under certain circumstances, I'd like it to simple ignore the triggers it missed firing.

However I can't seem to get this to happen. It does the processing and then quickly fires all the triggers it missed in quick succession. As I understand you can set a threshold for the misfires but I don't seem to be-able to get this working.

I'm using [DisallowConcurrentExecution()] on the Job to ensure only one instance of the job is running at any one time.

Below are a couple of snippets. First passing in some config info - is this how you set the misfire threshold?

    NameValueCollection config = new NameValueCollection();
    config.Add("quartz.jobStore.misfireThreshold", "600000");

    schedFact = new StdSchedulerFactory(config);

Building a trigger with what I assume to be the correct setting of Ignore misfires:

    var trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .StartNow()
                    .WithSimpleSchedule(   x => x.WithMisfireHandlingInstructionIgnoreMisfires()
                    .WithIntervalInSeconds(60)
                    .RepeatForever())
                    .Build();

Thoughts much appreciated.

Code for the Job: Just playing with rough ideas at this point so just running in a console app and randomly delaying a job so it shoots over the interval which is just set at 10 seconds for this. All the backed up misfires fire in quick succession after a couple of delays.

[DisallowConcurrentExecution()]
public class SomeJob : IJob
{
    public SomeJob() { }
    public void Execute(IJobExecutionContext context)
    {
        Random rnd = new Random(DateTime.UtcNow.Second);
        int delay = rnd.Next(2);
        Console.WriteLine("Executing Job with delay of "+ delay + "  at " + DateTime.UtcNow.ToString());

        if (delay == 1)
        {
            System.Threading.Thread.Sleep(1000 * 25); // sleep for 25 seconds
        }
    }
}





Example console output:
Executing Job with delay of 1  at 21/05/2015 21:27:17
Executing Job with delay of 1  at 21/05/2015 21:27:42
Executing Job with delay of 0  at 21/05/2015 21:28:07 <-- stacked misfires
Executing Job with delay of 0  at 21/05/2015 21:28:07 <--
Executing Job with delay of 0  at 21/05/2015 21:28:07 <--
Executing Job with delay of 0  at 21/05/2015 21:28:07 <--
Executing Job with delay of 0  at 21/05/2015 21:28:16
Executing Job with delay of 0  at 21/05/2015 21:28:26
Executing Job with delay of 1  at 21/05/2015 21:28:36
Executing Job with delay of 0  at 21/05/2015 21:29:01
Executing Job with delay of 0  at 21/05/2015 21:29:01
Executing Job with delay of 1  at 21/05/2015 21:29:06
4
  • Could you paste the code for your job?
    – LeftyX
    Commented May 21, 2015 at 13:05
  • Thanks for looking - I've updated the original post with code from the job. Commented May 21, 2015 at 21:11
  • Silly question: how can your job fire every 30 seconds when your trigger is set to fire every minute?
    – LeftyX
    Commented May 22, 2015 at 9:14
  • It's just test code, in the end I want to run the trigger once every minute as question stated and ignore any missed due to long running process. Inconsistencies above are because I'm currently testing with a 10 second interval. Commented May 22, 2015 at 15:08

1 Answer 1

36

WithMisfireHandlingInstructionIgnoreMisfires() is the wrong method for what you want, it doesn't mean the job will not trigger misfires, it means it will fire all triggers that were missed as soon as possible and will then go back to ordinary schedule. Which is what you see happening.

What you need is WithMisfireHandlingInstructionNextWithRemainingCount(). This will signal the scheduler to ignore misfires and wait for the next scheduled time. Misfiring strategies can be a bit confusing, for a concise explanation look here (it's for the java scheduler but it doesn't matter).

Even with the right strategy, you need to understand how the misfire threshold works- if you get it wrong your misfired trigger may still fire.

From the documentation: "A misfire is the time span by which a trigger must have missed its next-fire-time, in order for it to be considered "misfired" and thus have its misfire instruction applied".

You've set the value to 600000 milliseconds (and this value is for all triggers, unfortunately there is no threshold per trigger)- the misfire strategy will only be applied if your trigger fires 600000 milliseconds after the time it should have been fired. You can lower or increase it according to your requirement.

For more about the misfire threshold, read here.

4
  • Brilliant thank you, good link. Switching to WithMisfireHandlingInstructionNextWithRemainingCount and setting the misfire threshold to half the interval seems to do the job great. Commented May 22, 2015 at 16:17
  • @AbsolutelyN glad it worked, remember to upvote as well Commented May 22, 2015 at 16:17
  • 4
    That's got to be a Guinness record holder for longest method name.
    – Alan B
    Commented Jun 5, 2015 at 16:23
  • For some additional clarification on the org.quartz.jobStore.misfireThreshold: If when the scheduler starts, it finds jobs should have executed in the past, and they are not passed the org.quartz.jobStore.misfireThreshold, the scheduler will execute the jobs instantly, and without dellays between the executions if the jobs should have ran multiple times. This is why it is a really good idea to set the misfireThreshold smaller than the repeat time.
    – Alex G
    Commented Jan 14, 2022 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.