AutomaticRetryAttribute Class |
Namespace: Hangfire
The AutomaticRetryAttribute type exposes the following members.
Name | Description | |
---|---|---|
AutomaticRetryAttribute |
Initializes a new instance of the AutomaticRetryAttribute
class with DefaultRetryAttempts number.
|
Name | Description | |
---|---|---|
AllowMultiple | (Inherited from JobFilterAttribute.) | |
Attempts |
Gets or sets the maximum number of automatic retry attempts.
| |
LogEvents |
Gets or sets whether to produce log messages on retry attempts.
| |
OnAttemptsExceeded |
Gets or sets a candidate state for a background job that
will be chosen when number of retry attempts exceeded.
| |
Order | (Inherited from JobFilterAttribute.) |
Name | Description | |
---|---|---|
OnStateApplied |
Called after the specified state was applied
to the job within the given transaction.
| |
OnStateElection |
Called when the current state of the job is being changed to the
specified candidate state.
This state change could be intercepted and the final state could
be changed through setting the different state in the context
in an implementation of this method.
| |
OnStateUnapplied |
Called when the state with specified state was
unapplied from the job within the given transaction.
|
Name | Description | |
---|---|---|
DefaultRetryAttempts |
Represents the default number of retry attempts. This field is read-only.
|
Filter is added to the global Filters collection by default. Intervals between attempts are based on increasing exponential back-off multiplier in seconds.
This filter works in a state election phase by changing the candidate state from FailedState to the ScheduledState when another retry should be attempted, or other state based on the value of the OnAttemptsExceeded property when attempts exceeded.
The following example shows how to disable automatic retries for a specific job method by applying an attribute to a method.
Note |
---|
Even if you disable AutomaticRetryAttribute filter, your background jobs can still be executed several times, due to re-queue on shutdown and other compensation logic that guarantees the at least once processing. |
[AutomaticRetry(Attempts = 0)] public void RetriesDisabled(string arg) { }
The following example shows how to override the default number of retry attempts for all of the background jobs by modifying the global Filters collection.
// Call it in the application initialization logic GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 3 });
The following example shows how to ignore a background job when number of retry attempts exceed using the OnAttemptsExceeded property.
Tip |
---|
Choose Delete action when you aren't interested in processing background job that failed several times. |
[AutomaticRetry(OnAttemptsExceeded = AttemptsExceededAction.Delete)] public void DeleteWhenAttemptsExdeeded() { }