views:

208

answers:

4

I know that stackoverflow uses asp.net mvc but how they do background process (ie) processing a recent user updates,badges etc.... How to get started with background processing in asp.net mvc... ANy suggestion...

I just saw jeff's post

private static CacheItemRemovedCallback OnCacheRemove = null;

protected void Application_Start(object sender, EventArgs e)
{
    AddTask("DoStuff", 60);
}

private void AddTask(string name, int seconds)
{
    OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
    HttpRuntime.Cache.Insert(name, seconds, null,
        DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
        CacheItemPriority.NotRemovable, OnCacheRemove);
}

public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
    // do stuff here if it matches our taskname, like WebRequest
    // re-add our task so it recurs
    AddTask(k, Convert.ToInt32(v));
}

This background process works every sixty seconds... But i want to execute once in a day at 6.00 AM indian standard time (IST)... Is this possible?

+4  A: 

Now I doubt Jeff Atwood actually used this method when building Stack Overflow (well maybe for a while, who knows :P) but when I read his blog post regarding this method I was actually impressed by how something like Cache and it's expiry trigger could be used to do accomplish this.

Easy Background Tasks in ASP.NET

EDIT: Based on your updated question (I still think a service is the way to go though) you could be saving to the DB everytime you run the process to prevent multiple runs. Then at expiration of say every minute, you could check to see that if you around 6:00am and check that you the last time you ran it was greater than 23 hours and 55 minutes (roughly to account for a later completion timestamp). You could adjust the numbers based on the expected length of your process and make sure that you don't get an cache expiration overlap if that is even possible.

Just some thoughts as I have never actually tried to implement Jeff's solution, I just found it an interesting twist on using Cache.

Kelsey
+1  A: 

Since the cache.insert uses absolute expiration of given time, you cannot specify the exact time to trigger the task.

instead you can use the jeff's same approach, with the 60 sec expiration time, and check your server time in the onRemoveCallback delegate and trigger your job if this is met(if its 6.00 AM).

But i prefer to implement window service for this kind of work.

Cheers

Ramesh Vel
+1  A: 

If .NET 4/IIS 7 is an option, consider writing a Windows Server AppFabric service. These can be hosted in IIS, get automatic startup, monitoring, and an admin console for free. Having reinvented this wheel myself in the past, I can say this allows you to focus on your app instead of infrastructure.

Craig Stuntz
A: 

Be very careful with doing any background activity in ASP.NET. Remember that there is no single server process waiting around for your timer to go off - ASP.NET AppDomains restart for various reasons, and in general, your application could be hosted on multiple servers.

This kind of thing needs to occur in a separate service which is created for the purpose. Does your hosting provider permit WCF services? You could create a single-instance WCF service to do this that could be called from your ASP.NET application.

John Saunders