Search code examples
c#quartz.net

Trigger specific Quartz jobs


I am using Quartz to schedule jobs and use a console application to execute all the jobs.

I currently have 2 console applications which refer to the same set of Quartz tables viz. QRTZ_JOB_DETAILS, QRTZ_TRIGGERS etc.

Due to this, when I execute ConsoleApp1 which doesn't have jobs (created in ConsoleApp2), I get the following error:

XYZ job: Couldn't retrieve job because a required type was not found: Could not load type 'XYZ-Job, XYZ.Job' ---> Quartz.JobPersistenceException: 

I have checked the solution here.

Obvious solution is to create separate Quartz table-sets for each console application. That way, I won't get any load type errors.

My question is, in such a scenario, is there a way to get only particular jobs (based on some match), so that I don't need to create 2 table sets.

In the below code, I was thinking if I get all the job names, I will disable the triggers for ConsoleApp2. But then, ConsoleApp2 won't have any jobs to run! (this is because, the tables are same)

Please let me know if there is a better solution.

        protected async void StartScheduler1()
        {
                ISchedulerFactory schedFact = container.ResolveType<ISchedulerFactory>();
                var schedTask = schedFact.GetScheduler();

                schedTask.Wait();
                scheduler = schedTask.Result;

            var jobs = new List<JobKey>();
            foreach (var group in scheduler.GetJobGroupNames().Result)
            {
                var groupMatcher = GroupMatcher<JobKey>.GroupContains(@group);
                foreach (var jobKey in scheduler.GetJobKeys(groupMatcher).Result)
                {
                    jobs.Add(jobKey);
                }
            }

            scheduler.Start().Wait();
        }


Solution

  • Finally found a solution. There is a column called Sched_Name in Quartz tables. This column is used by Quartz scheduler to get job details. Using this column, we can have numerous different groups in the same Quartz tables. There is no need create separate Quartz table-sets. For e.g.

    SELECT * FROM QRTZ_JOB_DETAILS WHERE SCHED_NAME = 'CESA'

    SELECT * FROM QRTZ_JOB_DETAILS WHERE SCHED_NAME = 'CESB'