如何在Hangfire中取得背景工作的目前尝试次数? [英] How do I get the current attempt number on a background job in Hangfire?

查看:272
本文介绍了如何在Hangfire中取得背景工作的目前尝试次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Hangfire后台作业最后一次尝试结束之前,我需要执行一些数据库操作(我需要删除与该作业相关的数据库记录)

There are some database operations I need to execute before the end of the final attempt of my Hangfire background job (I need to delete the database record related to the job)

我当前的工作设置有以下属性:
[AutomaticRetry(Attempts = 5, OnAttemptsExceeded = AttemptsExceededAction.Delete)]

My current job is set with the following attribute:
[AutomaticRetry(Attempts = 5, OnAttemptsExceeded = AttemptsExceededAction.Delete)]

考虑到这一点,我需要确定当前的尝试次数是多少,但正在努力从Google搜索或Hangfire.io文档中找到有关这方面的任何文档.

With that in mind, I need to determine what the current attempt number is, but am struggling to find any documentation in that regard from a Google search or Hangfire.io documentation.

推荐答案

只需将PerformContext添加到您的工作方法中;您还可以从此对象访问JobId.对于尝试次数,它仍然依赖魔术字符串,但是它比当前/唯一的答案要轻巧一些:

Simply add PerformContext to your job method; you'll also be able to access your JobId from this object. For attempt number, this still relies on magic strings, but it's a little less flaky than the current/only answer:

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

这篇关于如何在Hangfire中取得背景工作的目前尝试次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆