Hangfire icon indicating copy to clipboard operation
Hangfire copied to clipboard

Multiple instances of AutomaticRetryAttribute are not allowed

Open FixRM opened this issue 8 months ago • 1 comments

Hello, and thank you for a great tool!

I'm trying to achieve two goals:

  1. All failed jobs must be transitioned to deleted state
  2. Retries must happen only for specific exception types

I tried the flowing:

GlobalJobFilters.Filters.Remove(typeof(AutomaticRetryAttribute));
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute()
{
    OnAttemptsExceeded = AttemptsExceededAction.Delete,
    OnlyOn = [ typeof(OperationCanceledException) ]
});
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute()
{
    Attempts = 0,
    OnAttemptsExceeded = AttemptsExceededAction.Delete
});

Retry works but jobs with other types of errors goes to "Failed" state instead of "Deleted".

Then I tried the following:

GlobalJobFilters.Filters.Remove(typeof(AutomaticRetryAttribute));
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute()
{
    Attempts = 0,
    OnAttemptsExceeded = AttemptsExceededAction.Delete
});
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute()
{
    OnAttemptsExceeded = AttemptsExceededAction.Delete,
    OnlyOn = [ typeof(OperationCanceledException) ]
});

In this case all failed jobs goes to deleted without any retry.

I guess the problem is that only one instance of each attribute type is enabled?

FixRM avatar Mar 27 '25 14:03 FixRM

@odinserj , if I may ask: Would a PR where one could have different timeouts per exception, would that be accepted, given it's of good quality?

regisbsb avatar Jun 24 '25 16:06 regisbsb