1

I am trying to get Quartz.net scheduler to work, but I don't know why it is not firing for jobs scheduled at a future date. I have tested with a cron trigger that fires every minute and it works (job and all), so I know it isn't a problem with my job code.

Things I have tried:

  1. Making the ISchedulerFactory a global static variable
  2. Making the IScheduler a global static variable
  3. I have added an email notification to the end of the Application_Start so I know when it is firing
  4. Every time I make changes to the scheduler code I restart the app and it fires my notification email, so I know it was restarted.

I am running this program on a shared hosting environment (not sure if that would have any effect on it). My guess (and this is only a guess) is something is being garbage collected, but I'm not sure what and why.

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    // construct a scheduler factory
    ISchedulerFactory schedFact = new StdSchedulerFactory();

    // get a scheduler
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();

    // construct job info
    JobDetail jobDetail = new JobDetail("myJob", null, typeof(Recorder));
    jobDetail.JobDataMap["domain"] = "www.mydomain.com";
    jobDetail.JobDataMap["userId"] = "2";

    // Create trigger (everything is in UTC!!!)
    CronTrigger cronTrigger = new CronTrigger("Schedule");
    cronTrigger.StartTimeUtc = TriggerUtils.GetEvenSecondDate(DateTime.UtcNow);
    cronTrigger.TimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");  // run in pacific timezone
    cronTrigger.CronExpressionString = "0 30 13 ? * MON-FRI *"; 

    sched.ScheduleJob(jobDetail, cronTrigger);
}
chobo
  • 31,561
  • 38
  • 123
  • 191
  • You may want to read this question and it's answers: http://stackoverflow.com/questions/1356789/quartz-net-with-asp-net – Chris Shain Feb 09 '12 at 17:08

1 Answers1

2

ASP.NET process can be shut down by IIS if no requests are coming in, so none of this code would be fired.

This is the reason why web-apps are not a good source of service-like (always running) behavior.

I've seen this implemented in a web-app with a page/web service which gets pinged via cURL tool from the outside.

If you're interested in debugging this further, add some notification in Application_End just to make sure that the process is actually shut down before the timer fires your scheduled job.

Leon
  • 3,311
  • 23
  • 20
  • The more I read into it the more I think that is what is happening. If the application process is shutdown would a new request trigger the application_start method? – chobo Feb 10 '12 at 17:43
  • Yes - if the IIS decides to shut down app pool due to inactivity, a new request would start the application again to fulfill the request. Look in IIS's AppPool settings for `Idle-Timeout` (default is 20 minutes). If no requests are made for 20 minutes, application pool might be shut down by IIS. You can extend this time as needed. – Leon Feb 10 '12 at 18:10
  • Another option, if you have no control of IIS (shared hosting) is to have something send a request to a "dumb" page via `curl` every so often, just to keep application from idle. – Leon Feb 10 '12 at 18:22