Friday, April 15, 2011

How to run automatic "jobs" in asp.net?

I want to have my website do a number of calculations every 10 minutes and then update a database with the results. How exactly do I set such a timer, i am assuming it would be in global.asax?

From stackoverflow
  • Check this out.

    Charlie Flowers : Now that is downright clever!
    Neil N : ha, clever indeed. gotta love some of the solutions that come up on Code Project
    Jonathan Allen : I have to say I distrust clever things.
    Neil N : true, this is more for people who are on hosted/shared servers who may not have the option to use SQL agent or windows task scheduler.
    çağdaş : @Neil N, you're right. This is a work around and not a great solution. But considering there's a lot of people working on ASP.NET && can't use task scheduler or SQL agent, it's worth mentioning.
  • You'd be better off writing a separate non-UI application and then running that as a scheduled task.

    Michael Todd : I agree. A web server is meant to serve a page, not peform a function at a scheduled time. Unless there's a reason (lack of access to the server, etc.), it should be a service.
    marc_s : Or a Windows service, if it has to run at all times, aroudn the clock, regardless of whether someone is logged in or not.
    ChrisF : @marc_s - you can set up scheduled tasks to run when no-one's logged into the machine.
  • the Cache solution in cagdas' answer works. I've used it. It's only downside is that it's difficult to turn it off if you need to suspend the timer for some reason. Alternate, but not quite identical solutions we've used.

    1. Scheduled tasks in SQL Server
    2. Scheduled windows tasks.
  • ifs its strictly database calculations, keep it in the database. Create a stored proc that does what you want, then have SQL Server agent run that proc on a schedule.

  • I really don't like schecduled tasks. I would rather put this function in a windows servic and throw a timer in it. With window services you can handle stop events very nicely. I do agree with everyone else, the web site is not the place for this.

    Neil N : a service seems more of a hassle than a scheduled task any day. A service is meant for something that "waits".
  • Doing something like that in a web application is somewhere between difficult and unstable to impossible. Web applications are simply not meant to be run non-stop, only to reply to requests.

    Do you really need to do the calculations every ten minutes? I have found that in most cases when someone asks a question like this, they really just need the appearence of something running at an interval, but as long as noone is visiting the page to see the results, the results doesn't really need to be calculated.

    If this is true in your case also, then you just need to keep track of when the calculations were done the last time, and for every request check if enough time has gone by to recalculate.

  • Aside from (correct) statements about instability of web application for scheduled task execution, here's a strategy you could implement:

    in global.asax, define application.onstart event in which create timer:

    var dueTime = 5000;
    var period = 5000;
    var MyTimer = new Timer(new TimerCallback(MyClass.MyTaskProc), null, dueTime, period);
    Application["MyTaskTimer"] = MyTimer;
    

    this will pretty much take care of creating task and restarting it should application exit

0 comments:

Post a Comment