Lock
Lock

Reputation: 5522

How do I reload all jobs and schedules in Quartz.Net periodically?

I am using Quartz.net within a Windows service to schedule jobs that exist within my database.

My users have will have a front end to this table whereby they can change the CRON expression. Currently, my application doesn't have any logic to change the CRON expression or add/delete jobs without having the service restarted (as this will trigger the reload of jobs). I am using the below code to load all the jobs from my database.

protected void LoadJobs()
{
    logger.Info("Loading jobs.");
    var jobs = middlewareRepository.GeActiveJobs();

    foreach (var job in jobs)
    {
        IJobDetail jobDetail = JobBuilder.Create<JobDespatcher>()
            .WithIdentity(job.Description)
            .UsingJobData("JobId", job.Id)
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithCronSchedule(job.CronExpression)
            .Build();

        scheduler.ScheduleJob(jobDetail, trigger);
        logger.InfoFormat("Job with id {0} ({1}) has been scheduled.", job.Id, job.Description);
    }
}

How would I go about clearing the scheduler queue and reloading the jobs.. say by checking every hour? Does Quartz.Net have a built in function to do this? Or would I have to build some logic that would unload all jobs/schedules and reload them? If I remove a job that is currently running, will that job be stopped or will it continue?

Any advice on the best way to implement this would be great.

Upvotes: 4

Views: 3763

Answers (2)

granadaCoder
granadaCoder

Reputation: 27906

First, I don't know why PinBack answer was downvoted. It is not an off the mark crazy answer.

Second, while I think PinBack was close, I will add some comments and code I would consider.

First a link to the API.

http://quartznet.sourceforge.net/apidoc/2.0/html/

Since there is no ".Refresh" method.....you might consider the below code..... If the scheduler already exists, it will gracefully shut it down. then it will force a "refresh" (aka, do a new call to GetScheduler), and start it, and return the newly refreshed scheduler.

    private IScheduler InitializeOrRefreshScheduler(IScheduler existingScheduler)
    {
        IScheduler returnSchedule = null;

        if (null != existingScheduler)
        {
            existingScheduler.Shutdown(true); /* gracefully shut down the scheduler http://quartznet.sourceforge.net/apidoc/2.0/html/html/31359f2c-7a57-50e8-5e8b-102ceb3991c9.htm */
        }
        else
        {
            /* the existingScheduler is null, so it would be null on "first startup" */
        }

        var config = (NameValueCollection)ConfigurationManager.GetSection("quartz");

        ISchedulerFactory factory = new StdSchedulerFactory(config);
        returnSchedule = factory.GetScheduler();    
        returnSchedule.Start();             

        return returnSchedule;
    }

Upvotes: 2

PinBack
PinBack

Reputation: 2574

I think you can shutdown the scheduler and start again.

scheduler.Shutdown();
//Recreate the scheduler
//Recreate jobs
scheduler.Start();

Upvotes: 3

Related Questions