How does the hangfire retry function work?

I used the repetitive work of Hangfire to execute my long process with a cron expression every minute (so basically it starts every minute to retrieve the data in db and do some processing), and I used the repetition of the Hangfire implementation of 3 when it fails .

Questions:

  • How does retry work inside Hangfire? Is repetitive work being performed every minute, and is retrying in parallel?
  • and how do I know if repetition 3 is already completed or has already been achieved (in code)?

The reason I want to know that the retry has already been reached, because I need to do something with my data in my database, processed by the repetitive Hangfire operation.

Note. Viewing the dashboard to search for a replay is not my option here, but through the code. Is it possible?

Implementation:

public class HangfireImpl
    {
        public static void ConfigureHangfire(IAppBuilder app, string dashBoardName, string connectionString)
        {
            GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 3 });
            GlobalConfiguration.Configuration
                    .UseSqlServerStorage(connectionString)
                    .UseDashboardMetric(SqlServerStorage.ActiveConnections)
                    .UseDashboardMetric(SqlServerStorage.TotalConnections)
                    .UseDashboardMetric(DashboardMetrics.FailedCount);

            app.UseHangfireDashboard(string.Format(@"/{0}", dashBoardName));
            app.UseHangfireServer();
        }

        public static void InitializeJobs()
        {
            // is there a way to find out how many retry occured inside my service.Execute?
            RecurringJob.AddOrUpdate<Worker>(service => service.Execute(), Cron.Minutely);
        }
    }
+4
source share
1 answer

I can already solve my problems.

Questions:

How does retry work inside Hangfire? Constant work every minute and repetition will be performed in parallel?

I guess the answer is YES while their available worker does your work.

and how would I know if repetition 3 is already done or already achieved (in code)?

I implemented JobFilterAttribute and IElectStateFilter and registered it using the global hangfire filter:

public class JobStateFilter : JobFilterAttribute , IElectStateFilter 
{
     public void OnStateElection(ElectStateContext context)
     {
          var failedState = context.CandidateState as FailedState;

           if (failedState == null) return;

          // I capture failed context here after 3 retry              
     }
}

GlobalJobFilters.Filters.Add(new JobStateFilter());
+2
source

Source: https://habr.com/ru/post/1620306/


All Articles