c# - hangfire 重试如何在 recuringjob 中工作?

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

我使用 Hangfire 的重复工作来完成我的长进程,每分钟都有 cron 表达式(所以基本上它每分钟运行一次以在数据库中获取数据并进行一些处理)并且我在失败时使用 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);
        }
    }

最佳答案

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

问题:

How does retry works inside Hangfire? Does recurring job every minute and the retry will run in parallel?

我想答案是,只要他们有可用的 worker 来执行您的工作。

and how would I know if the retry of 3 is already done or reached already (In code)?

我实现了 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());

关于c# - hangfire 重试如何在 recuringjob 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308605/

相关文章:

javascript - 按钮单击 Document.Ready() MVC 时出错

c# - 如何检测 Razor 页面中的回发?

c# - 当一个类没有实现必需的属性时,是否会抛出标准的 .NET 异常?

c# - GetUtcOffset 返回某些年份/日期的错误偏移量

.net - 文件被哪个进程锁定?

.net - 如何在命令行中从 .NET 程序集获取 IDL(或如何将 TLB 转换为 IDL)?

asp.net-mvc - YSlow 推荐。在 IIS 中,为什么默认情况下不选中启用内容过期

asp.net-mvc - 如何在MVC中给出特定模型的验证摘要?

c# - "Remember password"选项 [C#]

c# - .NET 图像是全黑的吗?