Click or drag to resize
AutomaticRetryAttribute Class
Represents a job filter that performs automatic retries for background jobs whose processing was failed due to an exception, with a limited number of attempts.
Inheritance Hierarchy

Namespace: Hangfire
Assembly: Hangfire.Core (in Hangfire.Core.dll) Version: 1.5.0.0
Syntax
public sealed class AutomaticRetryAttribute : JobFilterAttribute, 
	IElectStateFilter, IApplyStateFilter

The AutomaticRetryAttribute type exposes the following members.

Constructors
  NameDescription
Public methodAutomaticRetryAttribute
Initializes a new instance of the AutomaticRetryAttribute class with DefaultRetryAttempts number.
Top
Properties
  NameDescription
Public propertyAllowMultiple (Inherited from JobFilterAttribute.)
Public propertyAttempts
Gets or sets the maximum number of automatic retry attempts.
Public propertyLogEvents
Gets or sets whether to produce log messages on retry attempts.
Public propertyOnAttemptsExceeded
Gets or sets a candidate state for a background job that will be chosen when number of retry attempts exceeded.
Public propertyOrder (Inherited from JobFilterAttribute.)
Top
Methods
  NameDescription
Public methodOnStateApplied
Called after the specified state was applied to the job within the given transaction.
Public methodOnStateElection
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.
Public methodOnStateUnapplied
Called when the state with specified state was unapplied from the job within the given transaction.
Top
Fields
  NameDescription
Public fieldStatic memberDefaultRetryAttempts
Represents the default number of retry attempts. This field is read-only.
Top
Remarks

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.

Examples

Disabling Automatic Retries

The following example shows how to disable automatic retries for a specific job method by applying an attribute to a method.

Note 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.
C#
[AutomaticRetry(Attempts = 0)]
public void RetriesDisabled(string arg)
{
}

Overriding Defaults

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.

C#
// Call it in the application initialization logic
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 3 });

Specifying Attempts Exceeded Action

The following example shows how to ignore a background job when number of retry attempts exceed using the OnAttemptsExceeded property.

Tip Tip
Choose Delete action when you aren't interested in processing background job that failed several times.
C#
[AutomaticRetry(OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void DeleteWhenAttemptsExdeeded()
{
}
Thread Safety
Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.
See Also