I have an issue with Hangfire using version 1.6.19 atm with MongoDB as the storage, we currently have a method that is scheduled as follows:
BackgroundJob.Schedule(() => DoAsyncTask(parameters, JobCancellationToken.Null), TimeSpan.FromMinutes(X))
The task will run for over an hour and it contains a loop to validate when the job has completed. Inside the loop, there is a call to cancellationToken.ThrowIfCancellationRequested() to verify if a cancellation has been requested, but this call keeps getting fired approximately 30 minutes after the execution and terminates the job before completion.
I have been searching for information regarding this issue but most of it relates to older versions or the use of the InvisibilityTimeout which according to this answer has been deprecated, so I would like to know if anyone else has encountered this problem and any possible solutions.
Thank you
EDIT: After further investigation, I discovered the cancellation issue was only a side effect of the task being called again by HangFire after 30 minutes of running, and because I had validation set in place inside the method to avoid re-entry while the process was still running (to avoid duplication of data), the process would be treated as completed and therefore canceled.
So the real problem I face is I'm unable to determine the reason why HangFire keeps calling the process again after approximately 30 minutes of execution, I followed the steps described here to set the application on IIS to always run and prevent the pool from being recycled but the behavior persisted.