Search code examples
asp.netsql-servercronquartz.netvs-web-application-project

Scheduling concurrent jobs in quartz.net


I need some help. I am trying to figure out how to schedule dependent jobs with quartz.net and try to avoid conflicts as am using connection to database. i have a web application with 3 classes: DownloadJob, UploadJob, Table1-Table2Job. DownloadJob downloads items from a mysql db to an Sql db every day at 4am. UploadJob execute an update query to mysql db from an sql table(Table2) every 4 min. Table1-Table2Job is on charge of transferring items from an sql table to another sql table every 20 min.

The big problem is how to deal with connection conflicts. UploadJob must wait if Table1-Table2Job is in progress. and then go on. As Table1-Table2Job must wait if UploadJob is in progress. Download must always go.

i have create a Scheduler Class wish code is:

public class JobScheduler
{


    public static void Start()
    {
        Ul1_Ul2dbJob classU1_U2Job = new Ul1_Ul2dbJob();
        UploadJob classUpJob = new UploadJob();
        IScheduler schedulerDownload = StdSchedulerFactory.GetDefaultScheduler();

        IScheduler schedulerUl1_Ul2 = StdSchedulerFactory.GetDefaultScheduler();
        //schedulerUl1_Ul2.Start();
        IScheduler schedulerUpload = StdSchedulerFactory.GetDefaultScheduler();
        //schedulerUpload.Start();


        IJobDetail jobDown = JobBuilder.Create<DownloadJob>().Build();
        IJobDetail jobUpl = JobBuilder.Create<UploadJob>().Build();
        IJobDetail jobUl1_Ul2 = JobBuilder.Create<Ul1_Ul2dbJob>().Build();

        //Trigger di Download
        ITrigger trigger = TriggerBuilder.Create()
            .WithDailyTimeIntervalSchedule
              (s =>
                 s.WithIntervalInHours(24)
                .OnEveryDay()
                .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(03, 00))
              )
            .Build();
        //       ITrigger trigger =   TriggerBuilder.Create()
        //.WithCronSchedule(string.Format(cronFormat))
        //.Build();

        //trigger dei Upload
        ITrigger trigger1 = TriggerBuilder.Create()
           .WithCronSchedule("0 0/1 * * * ?")
           .StartNow()
           .WithPriority(1)
           .Build();
        //trigger per spostamento UL1 a UL2
        ITrigger trigger2 = TriggerBuilder.Create()
           .WithCronSchedule("0 0/1 * * * ?")
           .StartNow()
           .WithPriority(1)
           .Build();
        schedulerUpload.Start();
        schedulerUl1_Ul2.Start();
        schedulerUpload.ScheduleJob(jobUpl, trigger1);
        schedulerUl1_Ul2.ScheduleJob(jobUl1_Ul2, trigger2);
        //schedulerUpload.ScheduleJob(jobUl1_Ul2, trigger2);




        //// scheduling with use of function Lock()
        lock (schedulerDownload)
        {
            schedulerDownload.Start();
            schedulerDownload.ScheduleJob(jobDown, trigger);
        }
    }**

Some one can help me?


Solution

  • You could define a job which disallows concurrent executions (with the DisallowConcurrentExecution attribute). You will have a parameter in the data map to tell you if it's for uploading or transferring between tables. Then you schedule a job with the upload parameter every 4 minutes and the one with the transfer every 20. Quartz will make sure not to execute them at the same time so you don't have to do it yourself.