【问题标题】:How do I get the total retries on a background job in Hangfire from the PerformContext?如何从 PerformContext 获取 Hangfire 中后台作业的总重试次数?
【发布时间】:2018-06-14 14:55:09
【问题描述】:

来自这篇文章:

How do I get the current attempt number on a background job in Hangfire?

可以使用魔术字符串“RetryCount”来获取重试次数。

public void SendEmail(PerformContext context, string emailAddress)
{
   string jobId = context.BackgroundJob.Id;
   int retryCount = context.GetJobParameter<int>("RetryCount");
   // send an email
}

如果我需要获取总重试次数怎么办? 我可以使用类似的东西吗:

int retries = context.GetJobParameter<int>("Retries");

或者我如何从“PerformContext”(如果可能的话)中获取该信息?

我需要定义的总重试次数,因此我可以在最后一次重试时执行一些任务。

【问题讨论】:

    标签: c# hangfire


    【解决方案1】:

    如果您使用AutomaticRetryAttribute 来定义方法的重试次数,则如下所示:

    var retryAttempts = context.BackgroundJob.Job.Method.GetCustomAttributes(typeof(AutomaticRetryAttribute), false)
        .Cast<AutomaticRetryAttribute>()
        .Select(a => a.Attempts)
        .FirstOrDefault();
    

    如果该属性不存在,那么这将返回0。如果最大重试次数是全局设置的或稍后在属性中修改,这将无济于事。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2015-05-16
      • 1970-01-01
      相关资源
      最近更新 更多