Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Quartz.NET process synchronously?

I have a scheduled job that have repeat interval for every 5 mins. It's working fine.

But I have a situation in where my first job is not completing in 5 mins and a second job is starting (as it scheduled for 5 mins).

I don't want to do that, only one job should be working at a time. How can I do that?

This is my code:

ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
Trigger emailTrigger = TriggerUtils.MakeMinutelyTrigger(5);
emailTrigger.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
emailTrigger.Name = "EmailTrigger";
JobDetail emailJobDetail = new JobDetail("EmailJob", null, typeof(EmailJob));
sched.ScheduleJob(emailJobDetail, emailTrigger);
sched.Start();
like image 872
Neo Avatar asked Oct 09 '10 05:10

Neo


2 Answers

Quartz.NET 2.x

Implement IJob and also decorate your job class with [DisallowConcurrentExecution] attribute. Read the API documentation for DisallowConcurrentExecutionAttribute for more information.

Quartz.NET 1.x

Make the job class implement IStatefulJob rather than IJob. Read the API documentation for IStatefulJob for more information.

http://www.quartz-scheduler.net/documentation/faq.html#howtopreventconcurrentfire

like image 132
Marko Lahma Avatar answered Nov 05 '22 06:11

Marko Lahma


I've used Quartz.net a bit, but never investigated enforcing serial processing between jobs. From my experience, Quartz is more intended for "parallel" scheduled processing, where jobs can overlap if they run long, so I'm not sure if it supports what you need.

A simple solution to your problem might be to use a synchronization variable that can be accessed by any of the job threads (e.g. a lock, mutex, semaphore, or global boolean, etc.). When a new job starts up, it should check the lock, and if it's free, grab it, and hold it until it's finished. If another job wakes up, and sees that a previous job is still running, the new job can just exit, and wait for the scheduler to try again on the next interval. You could also have the new job wait for the previous to finish, but if you do that, you run the risk of jobs piling up waiting to execute, and the system never "catching up."

like image 35
Andy White Avatar answered Nov 05 '22 07:11

Andy White