I've got a report that does some heavy SQL queries; it can take over half an hour to run. So I've written it as a stored procedure that saves data to a table with a report key supplied by the client, and a status field indicating if the report is still in progress or complete. Now the client can invoke an API, which spins off a task thread, and the client can keep polling the API to see if the report is complete, then download the results. In other words, something like this:
public IHttpActionResult RunReport(ReportParameters parameters) {
Task.Run(() => _reportService.RunReport(parameters));
return Ok();
}
Trouble is, it seems that after a certain amount of time, the thread gets garbage collected; the spid running the report dies and the report status never gets updated.
I saw this question & answer which seems to relate to my issue. The API is continually active, not least because the client is checking back every 5 seconds to ask "Is it ready yet? And now? And now?" So it would seem that the thread shouldn't get garbage collected...but I can't argue with what I'm seeing, which is that the stored procedure simply dies in the middle, without raising any exceptions.
I could use Hangfire, of course, but that has its own overhead, and more moving parts to manage. Is there some other way to ensure the report thread doesn't die?