tags:

views:

46

answers:

2

I have a scheduled job that have repeat interval for every 5 mins. It's working fine. But I got situation in which 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? Below 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();

Thanks.

A: 

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."

Andy White
A: 

Make the job class implement IStatefulJob rather than IJob. Read the API documentation for IStatefulJob for more information. http://quartznet.sourceforge.net/faq.html#howtopreventconcurrentfire

Marko Lahma