How to ignore misfires in Quartz.Net?

This is my quartz configuration:

<quartz> <add key="quartz.scheduler.instanceName" value="EmailScheduler" /> <!-- Configure Thread Pool --> <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" /> <add key="quartz.threadPool.threadCount" value="10" /> <add key="quartz.threadPool.threadPriority" value="Normal" /> <!-- Configure Job Store --> <add key="quartz.jobStore.misfireThreshold" value="60000" /> <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" /> <add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz" /> <add key="quartz.jobStore.dataSource" value="default" /> <add key="quartz.jobStore.lockHandler.type" value="Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz" /> <add key="quartz.dataSource.default.provider" value="SqlServer-20" /> <add key="quartz.dataSource.default.connectionString" value="data source= ......" /> <add key="quartz.jobStore.tablePrefix" value="QRTZ_" /> </quartz> 

here is my IInterruptableJob :

 public class JobC : Quartz.IInterruptableJob { public void Interrupt() { Console.WriteLine("Job Interrupt() called at " + DateTime.Now); } public void Execute(IJobExecutionContext context) { // what code i should write here to detect misfires??? Console.WriteLine("FireTime at " + context.FireTimeUtc.Value + " PreviousFireTime at:" + (context.PreviousFireTimeUtc.HasValue ? context.PreviousFireTimeUtc.Value.ToString() : "NULL")); } } 

Here is my work and trigger:

 var job = JobBuilder.Create<JobC>().WithIdentity(new JobKey("JobC")).RequestRecovery(true).Build(); var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x .RepeatForever() .WithIntervalInSeconds(2) // I'm ignoring misfires here, but seems it not works! .WithMisfireHandlingInstructionIgnoreMisfires()) .StartNow() .Build(); var scheduler = new Quartz.Impl.StdSchedulerFactory().GetScheduler(); scheduler.Start(); scheduler.ScheduleJob(job, trigger); 

After calling scheduler.PauseAll() all tasks are suspended and after calling scheduler.ResumeAll() all the missed lights, it works! but I want to ignore them and just continue.

Thanks in advance.

+4
source share
3 answers

This may seem silly, but I made the following extension method to detect misfire inside the Execute method:

 public static bool IsMissedFire(this IJobExecutionContext context, int offsetMilliseconds) { if (!context.ScheduledFireTimeUtc.HasValue) return false; if (!context.FireTimeUtc.HasValue) return false; var scheduledFireTimeUtc = context.ScheduledFireTimeUtc.Value; var fireTimeUtc = context.FireTimeUtc.Value; return fireTimeUtc.Subtract(scheduledFireTimeUtc).TotalMilliseconds > offsetMilliseconds; } 

The use is simple:

 public void Execute(IJobExecutionContext context) { if (context.IsMissedFire(1000)) Console.WriteLine("Missfire"); else Console.WriteLine("Fire"); } 
+2
source

Are you sure your triggers match the misfire threshold? If you have a threshold of 60 seconds when you configure the states, each trigger that is not considered missed but has met the scheduled fire time will be launched as soon as possible after resuming.

So, you should see the ignore behavior if your pause lasts as the minimum threshold value, which in this case is 60 seconds.

+3
source

Although for the Java version of Quartz, the link below gives a good overview of the various skip skip policies:

http://nurkiewicz.blogspot.co.uk/2012/04/quartz-scheduler-misfire-instructions.html

In essence, WithMisfireHandlingInstructionIgnoreMisfires actually misfires as soon as the stream becomes available, which seems very inconsistent. To drop all misfires and continue the schedule, you need to use withMisfireHandlingInstructionNextWithRemainingCount.

+3
source

Source: https://habr.com/ru/post/1444514/


All Articles