background July 18, 2008

Easy Background Tasks in ASP.NET

As I work on the badge implementation for Stack Overflow, I needed a way to call the code that detects and awards the badges out of band. Traditionally this is done by something like cron or scheduled tasks. I’d rather have the code stay inside our current codebase, though. I asked on Twitter and got…
Avatar for Jeff Atwood
Co-Founder (Former)

As I work on the badge implementation for Stack Overflow, I needed a way to call the code that detects and awards the badges out of band. Traditionally this is done by something like cron or scheduled tasks. I’d rather have the code stay inside our current codebase, though.

I asked on Twitter and got some good responses, everything from “write a service” to “use threads”. I also got a link to Simulate a Windows Service using ASP.NET to run scheduled jobs. Now this is interesting — it’s just simple enough to work:

  1. At startup, add an item to the HttpRuntime.Cache with a fixed expiration.

  2. When cache item expires, do your work, such as WebRequest or what have you.

  3. Re-add the item to the cache with a fixed expiration.

The code is quite simple, really:

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

Works well in my testing; badges are awarded every 60 seconds like clockwork for all users.

Podcast logo The Stack Overflow Podcast is a weekly conversation about working in software development, learning to code, and the art and culture of computer programming.

Related

code-for-a-living November 23, 2020

The macro problem with microservices

In just 20 years, software engineering has shifted from architecting monoliths with a single database and centralized state to microservices where everything is distributed across multiple containers, servers, data centers, and even continents. Distributing things solves scaling concerns, but introduces a whole new world of problems, many of which were previously solved by monoliths.
Avatar for Ryland Goldstein
Head of Product at Temporal