6

I need to run a task repeatedly on an ASP.NET MVC site; it needs to run every time interval (Interval doesn't matter).

I achieved this by setting a background thread from the Global.asax.cs Application_Start which runs the task, sleeps for time interval and then runs again....and again.

While this works it doesn't seem like the right way to do this, seems a bit hacky.

So then I replaced it with a Timer object, this doesn't work reliably - seems others have had problems with it as well - so I'm not going to use this.

This needs to run in the MVC site, it is used for pulling jobs from a queue and rendering them from the Views ready for emailing.

I thought about using the ThreadPool and getting the previous job to leave another job on it's way out but because these can be long running jobs I've read that this can end up not leaving enough threads to deal with web requests through starvation.

Before sticking with the original thread method I thought I'd ask if anyone else knows a better way of doing this.

I hope this makes sense. Effectively what I'm trying to achieve is a heartbeat. Something to act as the consumer part of the producer/consumer pattern in an MVC site.

2 Answers 2

9

Stackoverflow itself uses (or at least used to use) a cunning way of doing this. See here: https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

In brief, you do this:

  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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm using this approach too in a production environment and I can also confirm that it does not cause any problems as long as you don't overdo it.
The page describing this technique is here: blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet
0

I know you've stated that:

This needs to run in the MVC site

However I think this is the wrong place for doing what you're trying to acheive.

Personally I'd set this up as a Windows Service and have the service check the database, compile the email as required, and add the email to a queueing service. The email service would then be seperate from this and would dispatch the emails at some interval.

What's stopping you at present from using another method than embedding this in your MVC site?

3 Comments

I know what your saying and I've thought of the same thing. It just seems such a simple thing to want to do that creating a new windows service just seems like a fair bit of messing to achieve it, it is another thing that can go wrong, etc. Also what if you don't have access to the box? Because the external sources that need to be aggregated are accessed async it won't overload the server it doesn't need to be done externally.
I think I'll add the HttpRuntime.Cache suggestion above. I just want sometime quick and dirty to get up and running. If the site reaches a point where it is no longer good enough then it will can probably be monetised at that point, will be worth having a rewrite and the proper solution implemented. Thank you for the answer though.
Glad to be of help Craig. Good luck with the venture!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.