Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Best Practice to Kick-off Maintenance Process on ASP.NET [closed]

Given an ASP.NET application, I need to run a maintenance process on a regular basis (daily, hourly, etc.).

What's the best way to accomplish this without relying on an external process like a scheduled task on the server (assume I don't have access to the server - shared hosting environment).

like image 493
Bramha Ghosh Avatar asked Sep 05 '08 13:09

Bramha Ghosh


1 Answers

Here's the way that StackOverflow does it:

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));
}

Details: [https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/][1] [1]: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

like image 129
John Rutherford Avatar answered Sep 22 '22 01:09

John Rutherford