1

I'm looking for solution to the following issue: I have a website and want that every one hour (approximately) will send mail to some address. So I decided to use Quartz library for C#. I wrote The job, the Start() function and the Execute:

public static void Start()
{
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    scheduler.Start();

    IJobDetail job = JobBuilder.Create<EmailJob>().Build();

    ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithIntervalInMinutes(50)
        .RepeatForever())
    .Build();

    scheduler.ScheduleJob(job, trigger);
}

In the Application_start():

 JobScheduler.Start();

When I run the website locally it works perfect, because the application is in "Play" mode all the time. so it's send the mail every 50 minutes as expected.

Now for the problem: When upload the website to the server, Job worked only one time - in the first time. After I read about it, my conclusion for this problem is that the website application have a "sleep" mode so if someone is not enter to the website, the application is sleep so the job is not working and the email doesn't sent.

So My question is - What can I do if I want a scheduler that will run function every 50 minutes, and not depend if someone entered the website or not?

Tomer Aro
  • 175
  • 1
  • 12
  • use some kind of site monitor to keep your site running all the time – Aristos May 28 '16 at 07:46
  • Possible duplicate of [Can you prevent your ASP.NET application from shutting down?](http://stackoverflow.com/questions/1306380/can-you-prevent-your-asp-net-application-from-shutting-down) – Ravi A. May 28 '16 at 07:52
  • Apart from that there is application initialization module http://www.iis.net/downloads/microsoft/application-initialization . However it works only on IIS 7.5 and greater and framework 4.0 and above. – Ravi A. May 28 '16 at 07:56

2 Answers2

0

Review your design. You're better off by creating a Console application and using the windows task Scheduler or writing a Windows Service Application

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
0

Depending on which version of IIS you are using it looks slightly different. But you need to set you application in alwaysrunning mode:
In your web.config add a applicationsPools section like this:

<applicationPools>
  <add name="appName" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />
</applicationPools>

Or you can change this in the IIS management by clicking on advanced settings on the application pool.

(Set the managedRuntimeVersion to your applications version of course)

Edit: As Ravi said, this have to be combined with a startup module or some external service keeping the site running.

Another solution could be to migrate to Hangfire and use IProcessHostPreloadClient together with my alwaysrunning approach.

Hypnobrew
  • 1,120
  • 9
  • 23
  • Actually this setting will only make sure that IIS spins a new w3wp while recycling or crash but it is not going to bring up the app domain and load the necessary assemblies and that's why you need application initialization module or external service like pingdom that generates a dummy request that brings up the appdomain and loads necessary assemblies. – Ravi A. May 28 '16 at 08:57
  • Hmm, that sounds right. The Quartz jobs will be rescheduled when the site restarts and could result in the jobs will never execute. I don't know if Quartz have any funcionality to handle this out of the box, but `Hangfire` do, using the `IProcessHostPreloadClient`. – Hypnobrew May 28 '16 at 09:15
  • Sorry wanted to edit as here - "Actually this setting will only make sure that IIS spins a new w3wp while recycling/crash/idletimeout but it is not going to bring up the app domain and load the necessary assemblies and that's why you need application initialization module or external service like pingdom that generates a dummy request that brings up the appdomain and loads necessary assemblies". – Ravi A. May 28 '16 at 09:40