Question

Just downloaded Quartz.Net, read the documentation which is out of date and have ended up with the code below which I believe is correct. (Let me know if it isn't)

I put this in my Application_Start of my ASP.Net application and the code gets executed but the job does not run. I think I read somewhere about setting Quartz up as a singleton but not sure if I've done that here?

I want to set this up to run daily at 9.00 but for now have used StartNow to check it works.

Please advise what I have to do?

    private void StartScheduler()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

        IScheduler scheduler = schedulerFactory.GetScheduler();
        scheduler.Start();

        IJobDetail jobDetail = JobBuilder
            .Create()
            .OfType(typeof(DBCleanUpJob))
            .WithIdentity(new JobKey("test", "1"))
            .Build();

        var trigger = Quartz.TriggerBuilder.Create()
                    .ForJob(jobDetail)
                     .WithIdentity(new TriggerKey("test", "1"))
                    .WithSimpleSchedule()
                    .StartNow()
                    .Build();
        //.WithDailyTimeIntervalSchedule(x=>x.StartingDailyAt(new TimeOfDay(09,00)));



        scheduler.ScheduleJob(jobDetail, trigger);
    }

public class DBCleanUpJob : IJob
{
    private IDocumentSession DocumentSession;

    public DBCleanUpJob(IDocumentSession DocSession)
    {
        DocumentSession = DocSession;
    }

    #region IJob Members

    public void Execute(IJobExecutionContext context)
    {
        throw new NotImplementedException();
    }

    #endregion
}
Was it helpful?

Solution

as you said, scheduler should be a singleton. with the code about scheduler is not a singleton and the scheduler only exists in the scope of the application starting, not the application running.

public static IScheduler Scheduler { get; private set; }

private void StartScheduler()
{
    Scheduler = new StdSchedulerFactory().GetScheduler();
    Scheduler.Start();

    var jobDetail = JobBuilder
        .Create()
        .OfType(typeof(DBCleanUpJob))
        .WithIdentity(new JobKey("test", "1"))
        .Build();

    var trigger = Quartz.TriggerBuilder.Create()
                .ForJob(jobDetail)
                .WithIdentity(new TriggerKey("test", "1"))
                .WithSimpleSchedule()
                .StartNow()
                .Build();
    //.WithDailyTimeIntervalSchedule(x=>x.StartingDailyAt(new TimeOfDay(09,00)));

    Scheduler.ScheduleJob(jobDetail, trigger);
}

and as Jehof pointed out. IIS will shutdown a website/application if there is no activity for a certain period of time.

Also note that your jobs will not have access to the asp.net pipeline. the jobs do not execute within the context of a request, therefore session, request, response, cookies are not available to the job.

Finally, if you want the scheduler to always run it will need to be independent of the website. Windows services are a good candidate. create a windows service project and have the scheduler start when the service starts. you could then setup quartz on the website to proxy jobs to the windows service. allowing the site to schedule jobs but the actual storage and execution is performed by the windows service scheduler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top