Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing an asynchronous task library for ASP.NET

Tags:

asp.net

The ASP.NET runtime is meant for short work loads that can be run in parallel. I need to be able to schedule periodic events and background tasks that may or may not run for much longer periods.

Given the above I have the following problems to deal with:

  • The AppDomain can shutdown due to changes (Web.config, bin, App_Code, etc.)
  • IIS recycles the AppPool on a regular basis (daily)
  • IIS itself might restart, or for that matter the server might crash

I'm not convinced that running this code inside ASP.NET is not the right thing to do, becuase it would allow for a simpler programming model. But doing so would require that an external service periodically makes requests to the app so that the application is keept running and that all background tasks are programmed with utter most care. They will have to be able to pause and resume thier work, in the event of an unexpected error.

My current line of thinking goes something like this:

If all jobs are registered in the database, it should be possible to use the database as a bookkeeping mechanism. In the case of an error, the database would contain all state necessary to resume the operation at the next opportunity given.

I'd really appriecate some feedback/advice, on this matter. I've been considering running a windows service and using some RPC solution as well, but it doesn't have the same appeal to me. And I'd instead have a lot of deployment issues and sycnhronizing tasks and code cross several applications. Due to my business needs this is less than optimial.

like image 461
John Leidegren Avatar asked Oct 06 '10 05:10

John Leidegren


2 Answers

This is a shot in the dark since I don't know what database you use, but I'd recommend you to consider dialog timers and activation. Assuming that most of the jobs have to do some data manipulation, and is likely that all have to do only data manipulation, leveraging activation and timers give an extremely reliable job scheduling solution, entirely embedded in the database (no need for an external process/service, not dependencies outside the database bounds like msdb), and is a solution that ensures scheduled jobs can survive restarts, failover events and even disaster recovery restores. Simply put, once a job is scheduled it will run even if the database is restored one week later on a different machine.

Have a look at Asynchronous procedure execution for a related example.

And if this is too radical, at least have a look at Using Tables as Queues since storing the scheduled items in the database often falls under the 'pending queue' case.

like image 55
Remus Rusanu Avatar answered Nov 15 '22 04:11

Remus Rusanu


I recommend that you have a look at Quartz.Net. It is open source and it will give you some ideas.

like image 35
kgiannakakis Avatar answered Nov 15 '22 05:11

kgiannakakis