Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add job with trigger for running Quartz.NET scheduler instance without restarting server?

Is it possible to add job with trigger for running Quartz.NET scheduler instance without restarting server?

like image 536
GJgappiya Avatar asked Jun 20 '14 06:06

GJgappiya


1 Answers

A fairly robust implementation with ADOJobStore is to have a custom table to store jobs and create a class that inherits from ISchedulerPlugin and IJob to create schedules for your job automatically.

Your config will look like this:

<add key="quartz.plugin.sqlquartzjobs.type" value="(JobSchedulerPlugin assembly path)" />
<add key="quartz.plugin.sqlquartzjobs.RescanCronExpression" value="0 0/5 * * * ?" /> //plugin should fire every five minutes
<add key="quartz.plugin.sqlquartzjobs.ConnectionString" value="(your connection string)" />

Your plugin/job class can look like this:

public class JobSchedulerPlugin : ISchedulerPlugin, IJob
{
        //Entry point for plugin, quartz server runs when it starts
        public void Initialize(string pluginName, IScheduler sched)
        {
            Name = pluginName;
            Scheduler = sched;
        }

        //Runs after Initialize()
        public void Start()
        {
                //schedule plugin as a job
            JobDataMap jobData = new JobDataMap();
            jobData["ConnectionString"] = ConnectionString;

            IJobDetail job = JobBuilder.Create(this.GetType())
                .WithDescription("Job to rescan jobs from SQL db")
                .WithIdentity(new JobKey(JobInitializationPluginJobName, JobInitializationPluginGroup))
                .UsingJobData(jobData)
                .Build();

             TriggerKey triggerKey = new TriggerKey(JobInitializationPluginJobTriggerName, JobInitializationPluginGroup);

             ITrigger trigger = TriggerBuilder.Create()
                 .WithCronSchedule(ConfigFileCronExpression)
                 .StartNow()
                 .WithDescription("trigger for sql job loader")
                 .WithIdentity(triggerKey)
                 .WithPriority(1)
                 .Build();

             Scheduler.ScheduleJob(job, trigger);
        }
}

Now JobSchedulerPlugin has entered a trigger into QRTZ_TRIGGERS that will fire every five minutes with highest priority. You can use it to load jobs from your custom table (let's call it QUARTZJOBS). QUARTZJOBS can contain information such as jobnames, assembly paths, dates, status, etc, anything that can be used to help you create triggers efficiently. It should also contain the cron expression to the job. This is what you can do when the trigger fires:

//Entry point of every job
public void Execute(IJobExecutionContext context)
{
    Scheduler = context.Scheduler;

    JobCollection jobs = LoadJobs(context.JobDetail.JobDataMap["ConnectionString"].ToString());
    JobsWithTriggers jobTriggers = CreateTriggers(jobs);
    SchedulerJob(jobTriggers);
}

//You can use ADO.NET or an ORM here to load job information from the the table
//and push it into a class. 
protected JobCollection LoadJobs(string connectionString);

//In this class you can create JobDetails and ITriggers for each job
//and push them into a custom class
protected JobsWithTriggers CreateTriggers(jobs);

//Finally here you can schedule the jobs
protected void ScheduleJobs(jobstriggers)

In each of the classes above you can add custom validation for making sure triggers are handled appropriately if status or cron expression changes.

With this solution the server will never need to be restarted. The plugin/job class will scan the table and act accordingly.

like image 188
Nick Patsaris Avatar answered Nov 16 '22 20:11

Nick Patsaris