我已经使用了Hangfire的重复工作来完成我的每一分钟cron表达式的长时间进程(所以基本上它每分钟运行一次以获取db中的数据并进行一些处理)并且当它失败时我使用了3次Hangfire实现重试.
问题:
>在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?
我猜答案是肯定的,只要他们是可用的工作人员来执行你的工作.
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());