Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep a task alive after the method that called it has exited?

I'm writing a webservice that drops off a long-running bulk insert command to a sql db through a stored proc. I don't want the webservice hung up while waiting for a response from the db, so I'd like to just return an http response that lets the client know the request has been sent to the db after I start the task. But as soon as I return the response, the task will lose context and get trashed, right? How should I keep this alive?

like image 492
will Avatar asked Nov 26 '25 07:11

will


1 Answers

In general, it's not a good idea to spin off something to do work from IIS. What happens if the AppPool restarts? What happens if there is an exception?

Instead, I would recommend writing a Windows Service and have it responsible for the work.

Based on your comments, I would see if you can ask for the following requirements (theoretically):

All external calls are done through the web service. The web service uses a separate assembly for the actual data access.

A separate windows service is used for long running processes, which would also use the same data access assembly the web service uses.

That is really the best way to go (but not necessarily doable based on requirements).

like image 104
Erik Philips Avatar answered Nov 28 '25 22:11

Erik Philips