Prevent running multiple scheduled tasks at a time/only one at a time (pause while other is running)

654 Views Asked by At

I have two scheduled tasks created using Quartz.NET library like following:

 public static void StartCompetitorResearchUpdate()
    {
        IJobDetail competitorResearch = JobBuilder.Create<CompetitorResearchJob>().WithIdentity("Job1").Build();
        ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger1").StartNow().WithCronSchedule("0 0 0/2 * * ?").ForJob("Job1").Build();
        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sc = sf.GetScheduler();
        sc.ScheduleJob(competitorResearch, trigger);
        sc.Start();
    }
    public static void StartStoreAnalyticsUpdate()
    {
        IJobDetail competitorResearch = JobBuilder.Create<StoreAnalyticsUsersUpdate>().WithIdentity("Job2").Build();
        ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger2").StartAt(DateTimeOffset.UtcNow).WithCronSchedule("0 0 0/4 * * ?").ForJob("Job2").Build();
        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sc = sf.GetScheduler();
        sc.ScheduleJob(competitorResearch, trigger);
        sc.Start();
    }

The trick here is that I'd like that first scheduled task StartCompetitorResearchUpdate() <= to pause for xx minutes ( preferrably like 20-30 minutes - or while 2nd job is running) because the first job can sometimes run 24/7 and theres a lot of data to be pulled via HTTP requests, that's why I need to pause the first one while 2nd one runs.

Is there any way to achieve this ?

P.S. the first job runs every 2 hours, and 2nd job runs every 4 hours...

1

There are 1 best solutions below

0
Rabban On

You can use ITriggerListener and implement the method VetoJobExecution. More informations can be found here.