In our application, we’re using Hangfire to queue up long running database queries. However, on deleting the job, it doesn’t cancel the query. Doing some research, I have found that we need to pass IJobCancellationToken into our long running job when we run BackgroundJob.Enqueue()
, IE
BackgroundJob.Enqueue(() => LongRunningMethod(JobCancellationToken.Null));
and then we are supposed to run:
public void LongRunningMethod(IJobCancellationToken cancellationToken)
{
//Code here
cancellationToken.ThrowIfCancellationRequested();
}
However, all that is occurring in the Code here
comment is a _db.Execute(someCommand);
There is an async version of method called _db.ExecuteAsync(someCommand, CancellationToken);
but I am not sure how to hook up the IJobCancellationToken
to cancel the async CancellationToken
. Is something like this possible? What’s the approach here?