I have an API to generate a long running task and a different API to fetch the result (can also be called to get a partial result).
An example:
[HttpGet]
public IActionResult LongRunning()
{
var taskId = longRunningService.CreateTask();
var progress = new ProgressDto
{
Id = taskId,
Status = "created"
};
return CreatedAtAction("LongRunningProgress", new { id = taskId }, progress);
}
[HttpGet("{id:int}")]
public async Task<ActionResult<ProgressDto>> LongRunningProgress(int id)
{
var task = longRunningService.GetTask(id);
// Return based on task completionstate or partial result
}
[HttpDelete("{id:int}")
public ActionResult CancelLongRunning(int id)
{
// How?
}
I liked the way this was strung together and made sense from a REST point of view. However I can’t figure out how to cancel the task properly.
One way is for the service to also know about the CancellationTokenSource
but that feels wrong?
Another way is for the Controller to know about a static CancellationTokenSource
but that limits the controller to a single long running task at a time (which obviously is not feasible).
I know you can pass a cancellation token into the controller action, but that is for that singular action. It cannot be used for a different action. And it will be triggered on disconnect which is not a case here.
Is there an obvious answer I’m missing?
Thanks