0

I would like to run a time consuming script (to update a database from a 3rd party API) at regular intervals. I was wondering what the best practice for this would be:

  1. What should the 'script' be - an ASP.NET service? Bearing in mind I am on shared hosting, this may not be possible (but I would like to know).
  2. How could the script be scheduled to run at regular intervals/at set time automatically?

Thanks in advance!

James
  • 30,496
  • 19
  • 86
  • 113

3 Answers3

1

Some options for this:

  • Use a separate thread that keeps running all the time - and does the update on time (and then sleeps).
  • Use a timer and trigger the update event.
  • Use a Cache expiration trigger, but test this so that it keeps running without users visiting the site.

I would suggest checking out http://www.beansoftware.com/ASP.NET-Tutorials/Scheduled-Tasks.aspx for more details on these methods.

Jontas
  • 409
  • 3
  • 12
1

There is no way you can guarantee that something runs e.g. every night in a normal IIS setup. Batch jobs are thus a pain to handle. The only "mode" of execution for IIS is requests. If your application has no requests it doesn't run at all so IIS does not spend any resources on executing code in your application, i.e. it can unload it entirely.

If you have your own host, you would typically create a windows service to run your background tasks. I believe the same is possible in Azure. But for a standard sharesd IIS host, you basically can't setup a scheduled background task.

One of the simplest hacks is to setup a protected service that executes the job when it gets a request. Then you can make sure an external caller calls into your service at the required intervals.

Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
  • I am going to give @Romil's method a go but unfortunately this may prove you right! The external caller method is definitely a possibility, but I guess if the site got popular enough it could use user's request to trigger the service at the right time. – James Jun 04 '12 at 10:41
  • 1
    See this answer as well http://stackoverflow.com/questions/838318/how-to-keep-asp-net-assemblies-in-appdomain-alive – Anders Forsgren Jun 04 '12 at 13:19
  • I am personally with GoDaddy, they give me a button under IIS management 'Recycle App Pool'. I presume the existence of this button means that they don't constantly recycle it - other than when mem leaks/errors occur. Should this make me hopeful some of the above methods will work? – James Jun 04 '12 at 14:16
0

What you can do is add a System.Timers.Timer in Global.asax.

System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerElapsed), null, new Timespan(0), new Timespan(24, 0, 0));

// This will run every 24 hours.
private void TimerElapsed(object o)
{
    // Do stuff.
}

In IISManager, enable HTTP-Keep Alives for your application.

In IIS Manager, select Http Response Headers Module, open it and in the Actions Pane, select Set Common Headers and in there select Enable Http Keep Alives.

Also, check for a setting of your application pool -

Select the application pool of your application, select Advanced Settings from the right Actions Tab.

In there there is a setting called - Idle Timeout (minutes)

By default it is 20 Minutes. Make it something like 60 Minutes or increase it even more and check.

Romil Kumar Jain
  • 20,239
  • 9
  • 63
  • 92