0

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?

LeftyX
  • 35,328
  • 21
  • 132
  • 193

2 Answers2

1

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.

Alioza
  • 1,690
  • 12
  • 14
0

You can check which jobs are running by calling GetCurrentlyExecutingJobs(). However, this method is not cluster aware, so it won't be able to see jobs scheduled by other schedulers. It's best if you schedule all your jobs using the same scheduler.

Assuming you do that, you can just do the following from UploadJob and Table1-Table2Job

    public void Execute(IJobExecutionContext context)
    {
        var jobKey = new JobKey("otherJob", "group1"); 

        while (IsOtherJobRunning(context.Scheduler, jobKey)) // if job is running
        {
            Thread.Sleep(60000); // sleep for one minute
        }

        // continue with task.
    }

    /// <summary>
    /// Checks whether a job with the supplied job key is current running
    /// </summary>
    /// <returns>true/false</returns>
    public bool IsOtherJobRunning(IScheduler scheduler, JobKey key)
    {
        var jobs = scheduler.GetCurrentlyExecutingJobs();

        foreach (var job in jobs)
        {
            if (job.JobDetail.Key.Name == key.Name)
            {
                return true;
            }
        }

        return false;
    }
Nick Patsaris
  • 2,118
  • 1
  • 16
  • 18