IIS7 is it possible to get it to run a webpage every 5 mins?
Question
I have IIS7.5. We currently have a weighted rating for entities on our website. Calculating the weighted rating is extremely slow, to the point loading the homepage now takes more than 10 seconds to load.
To solve this, I'd like to store the weighting in the database with each entity, and have IIS run a script every 5-10 minutes that recalculates the weightings.
How do I go about doing this? It would be easiest for me if it ran a webpage URL.
Solution
To not use client, only server to continuously call this function, you can create a thread on the server to call the function that calculates it.
A better way to start the thread or timer is in the Global.asax, like this sample:
public class Global : System.Web.HttpApplication
{
private Timer _timer;
void Application_Start(object sender, EventArgs e)
{
int period = 1000 * 60 * 10; // 10 minutes
_timer = new Timer(TimerCallback, null, 1000, period);
}
private void TimerCallback(object state)
{
// Do your stuff here
}
}
}
OTHER TIPS
One approach is to use a Windows service for this rather than calling a web URL.
This can then run completely out-of-band in the background to perform calculations. Details on this are here:
http://msdn.microsoft.com/en-us/library/d56de412%28v=VS.100%29.aspx
A few advantages include:
- Your IIS process will not be affected, so your users will see no slowdown
- The service can be stopped or started independently of the Web site
However, you'll need to have reasonably full access to the server to install and run the service.
You can use a Cache
entry for this, set to expire 10 minutes in the future.
When you add the item, use a callback function for the CacheItemRemovedCallback
parameter - in this callback function do your database work and re-add the expiring cache entry.
Other options include:
- Using one of the timer classes included in the BCL - there is a MSDN magazine article describing and comparing the different ones.
- Writing a windows service to do this.
- Using a scheduled task.
Windows service and schedules tasks still require you to have some way to communicate the results to IIS.
I have done something like this earlier and I had used windows scheduled tasks to call my script at specific intervals of time.
A simple batch file with WGET or similar, and help from Scheduled Tasks will do it.
http://www.gnu.org/software/wget/
you can try this to test this idea:
wget http://localhost/filename.ashx