Chris
Chris

Reputation: 28064

ASP.NET auto-executing page

I've long toyed with the idea of some kind of auto-cleanup page that performs routine maintenence tasks and is called out of scope of the current page execution by means of calling a 1x1 pixel gif with the asp.net page as the src parameter. One thing I've never really decided on, however, is how to handle the timing of such a page's execution so that it doesn't execute with every request, maybe like.. every 5th request. or every 30 minutes or some other interval.

How would you consider building such a mechanism?

Upvotes: 3

Views: 897

Answers (5)

Corbin March
Corbin March

Reputation: 25704

Related entry from the stackoverflow blog : https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/.

Others:

Note a few solutions don't work if you lack steady, consistent traffic. If IIS unloads your app, Cache, Session, and Request approaches don't work.

If it's possible, install a Windows task service per service. Work out a scheme to add/remove tasks dynamically.

If your app is hosted and you can't install a service, consider hitting it with a site monitor - something like siteuptime. This keep your app in memory, allows you to hit a handler or action page versus a dummy image, and lets you decide exactly when it runs.

Upvotes: 1

Maxam
Maxam

Reputation: 4051

Have you considered using a windows service instead? Running your cleanup tasks would take up a thread from the ASP.NET thread pool. Or if you're cleaning up a SQL Server database, maybe a SQL Server Agent job would be better.

Upvotes: 1

Anjisan
Anjisan

Reputation: 1799

It sounds like you might want to use the global.asax file instead - assuming you want something to happen every Nth time. For example, if you wanted to do something for every 5th visitor to your site, you could do something along the lines of,

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    Application["Sessions"] = 0;
}

void Session_Start(object sender, EventArgs e) 
{
   // Code that runs when a new session is started
   Application.Lock();
   Application["Sessions"] = (int)Application["Sessions"] + 1;

   if ((int)Application["Sessions"] % 5 == 0)
   {
     DoSomething();
     Application["Sessions"] = 0;
    }
    Application.UnLock();
}

I would heavily recommend something along these lines rather than attaching a script to the source of a GIF file. You have much more control and security.

Upvotes: 4

John Lemp
John Lemp

Reputation: 5067

I have come across this situation many times and I generally just end up using task scheduler to just call the page, that way it is consistent and reliable. The problem with relying on a page to be called is that you have to be sure that there will always be requests to your page. If you can guarantee that, then just store a variable at the application level with the timestamp of the last time the task was run and have every page check that timestamp and update it once a request comes in that has passed a certain threshold.

Upvotes: 4

jdigital
jdigital

Reputation: 12266

I would set a timer and run the cleanup task when the timer fires. If you'd like to base the cleanup on the number of requests, you could make a shorter timer interval and then check the request count when the timer fires; if the request count is too low, then skip the cleanup.

Upvotes: 0

Related Questions