【问题标题】:How does hangfire retry works in reccuringjob?hangfire 重试如何在 recuringjob 中工作?
【发布时间】:2015-12-16 09:46:06
【问题描述】:

我已经使用 Hangfire 的重复工作来执行我的长过程,每分钟的 cron 表达式(所以基本上它每分钟运行一次以在 db 中获取数据并进行一些处理),并且当它失败时我使用了 Hangfire 实现重试 3。

问题:

  • 重试如何在 Hangfire 中工作?每分钟重复一次作业和重试是否会并行运行?
  • 我怎么知道 3 的重试是否已经完成或已经达到(在代码中)?

我想知道是否已经达到重试的原因是因为我需要对我的数据库中的数据做一些事情,这些数据正在通过 Hangfire 的重复工作进行处理。

注意:查看仪表板以查找重试不是我的选择,而是通过代码。有可能吗?

实施:

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);
        }
    }

【问题讨论】:

    标签: c# .net asp.net-mvc hangfire


    【解决方案1】:

    我可能已经解决了自己的问题。

    问题:

    重试在 Hangfire 中是如何工作的?每分钟重复一次工作 并且重试将并行运行?

    我想答案是YES,只要他们有空的工人来执行你的工作。

    我怎么知道 3 的重试是否已经完成或达到 已经(在代码中)?

    我实现了 JobFilterAttribute 和 IElectStateFilter 并使用 hangfire 的全局过滤器注册它:

    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());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多