I am using Hangfire to upload data of excel file(small-large) to database table(Insert\update) as background process in Asp.Net MVC web application.
I also need to keep checking if the excel records uploaded to database within 1 minute then I display success message and if it takes more time then one minute then I will display message that your process will keep running in background and user can navigate to other pages.
I am using below code to get status while polling upto 1 minute :
var monitoringApi = JobStorage.Current.GetMonitoringApi();
var isSucceed = monitoringApi.SucceededJobs(0, System.Convert.ToInt32(monitoringApi.SucceededListCount())).FirstOrDefault(t => t.Key == jobId);
if (isSucceed.Key != null)
{
return true; // Job has succeeded
}
return false;
My problem is this fetching of job status becomes very slow once the jobs table has some more rows like 50 or more then that.Even it become slow in SQL sever management studio’s query window.
simple query like below takes 50 seconds to return results (jobs table has just 40 rows):
select top 10 * from HangFire.Job
The method that I am passing to BackgroundJob.Enqueue has parameter which contains all the data of excel file.(it has more then 30000 rows sometimes)
(This parameter value is going in Job table’s arguments field.)
Please suggest further so that I can improve performance of the query/checking the status.
You could store the excel data in another table and pass only the row Id to your Hangfire job. It’s the prefferred way to having huge arguments that will have to be serialized.
Thanks a lot for quick reply. I have modified my method and its parameters and looks better then before.
One more question, how actually Hangfire processes jobs? For example, first it puts the job in “Enqueue” then “Processing” and if all good,then “Succeeded” state. But if any error comes in executing job then it moves it to “Scheduled” state, after this how it will handle job processing? My AutomaticRetry is set to 0 right now but please suggest for both the case when AutomaticRetry is enabled.
You are right - Hangfire is basically a state machine that moves jobs between states. If an exception occurs during execution, it will reschedule the job with increasing interval until the RetryAttempts limit is reached. Then it will mark it as failed (default) or delete it, based on the AutomaticRetry.OnAttemptsExceeded property.
Thanks for reply. Just confirming what I have understood from the last answer, So for me, as I have kept AutomaticRetry to 0, If there is some exception in job, it will go to “Scheduled” state and it will not retry for it and will be in failed state after some time. Then I have to delete it maually or using AutomaticRetry.OnAttemptsExceeded property.
So if above understanding is ok then,
How much time it can take to go from “Scheduled” to “Failed” state as my AutomaticRetry is set to 0.
As AutomaticRetry is set to 0, should it not go to direct “Failed” ? Why “Scheduled” state in between?
When AutomaticRetry is set to 0, a background job will go to the Failed state or to the Deleted state directly, depending on OnAttemptsExceeded property value.