Table of Contents

Class RetryPolicyAttribute

Namespace
Quartz
Assembly
Quartz.dll

How the scheduler retries a failed firing of this job when the trigger that fired it has no RetryPolicy of its own. Overrides the scheduler's default, which UseDefaultRetryPolicy sets.

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
public sealed class RetryPolicyAttribute : Attribute
Inheritance
RetryPolicyAttribute
Inherited Members

Examples

[RetryPolicy(5, "00:00:30", 2, MaxDelay = "00:10:00")]
public sealed class ImportJob : IJob
{
    public async ValueTask Execute(IJobExecutionContext context, CancellationToken cancellationToken = default)
    {
        await Import(cancellationToken);
    }
}

Remarks

Declared on the job rather than stored with it, for the reason JobTimeoutAttribute is: whether a job is safe to retry is a property of its code, and an attribute travels with the type through every job store, wire format and way of scheduling. Nothing is written to a trigger's row: the policy is looked up each time a firing fails, so it covers triggers stored before it was declared. It is inherited from a base class or from an interface the job implements, and the type's own declaration wins over an interface's.

The constructors are the factories of RetryPolicy, with each TimeSpan spelled as a string in its invariant form, because a TimeSpan cannot be an attribute argument:

AttributePolicy
[RetryPolicy(3, "00:05:00")]Fixed(int, TimeSpan)
[RetryPolicy(5, "00:00:30", 2, MaxDelay = "00:10:00", Jitter = 0.2)]Exponential(int, TimeSpan, double, TimeSpan?, double)
[RetryPolicy("00:00:10", "00:01:00", "01:00:00")]Explicit(params IReadOnlyList<TimeSpan>)
[RetryPolicy(0)]None: never retried, whatever the scheduler's default

An argument that is not a policy is refused. The attribute throws as it is read, and the scheduler reads it when the job is added or scheduled, so AddJob and ScheduleJob fail with a SchedulerException naming the job type.

Constructors

RetryPolicyAttribute(int)

Declares that the job is never retried: None.

public RetryPolicyAttribute(int maxAttempts)

Parameters

maxAttempts int

0. Any other count needs a delay.

Exceptions

ArgumentOutOfRangeException

maxAttempts is not 0.

RetryPolicyAttribute(int, string)

Declares Fixed(int, TimeSpan): the same wait before every retry.

public RetryPolicyAttribute(int maxAttempts, string delay)

Parameters

maxAttempts int

How many times to retry after the first failure; at least one.

delay string

The wait before each retry, as an invariant TimeSpan: "00:05:00" is five minutes.

Exceptions

ArgumentException

delay is not a TimeSpan.

ArgumentOutOfRangeException

maxAttempts is less than one, or delay is negative.

RetryPolicyAttribute(int, string, double)

Declares Exponential(int, TimeSpan, double, TimeSpan?, double): a wait that grows by factor with every retry. MaxDelay and Jitter are its other two arguments.

public RetryPolicyAttribute(int maxAttempts, string initialDelay, double factor)

Parameters

maxAttempts int

How many times to retry after the first failure; at least one.

initialDelay string

The wait before the first retry, as an invariant TimeSpan.

factor double

What each wait is multiplied by to get the next one; at least 1.

Exceptions

ArgumentException

initialDelay is not a TimeSpan.

ArgumentOutOfRangeException

maxAttempts is less than one, initialDelay is negative, or factor is less than one or not a number.

RetryPolicyAttribute(params string[])

Declares Explicit(params IReadOnlyList<TimeSpan>): the given waits, in order, one per retry.

public RetryPolicyAttribute(params string[] delays)

Parameters

delays string[]

The wait before each retry, each an invariant TimeSpan; at least one, none negative.

Exceptions

ArgumentNullException

delays is null.

ArgumentException

delays is empty, holds something that is not a TimeSpan or a negative one, or is too long for the triggers table's retry policy column.

Properties

Jitter

How far either side of each computed wait a retry may land, as a fraction of it between 0 and 1. Only the exponential form takes one.

public double Jitter { get; init; }

Property Value

double

Remarks

Read from Policy, which holds it: 0 unless it was given.

Exceptions

ArgumentException

The attribute is not the exponential form.

ArgumentOutOfRangeException

The value is outside [0, 1].

MaxDelay

The ceiling every computed wait is clamped to, as an invariant TimeSpan, or null to let them grow unbounded. Only the exponential form takes one.

public string? MaxDelay { get; init; }

Property Value

string

Exceptions

ArgumentException

The value is not a TimeSpan, or the attribute is not the exponential form.

ArgumentOutOfRangeException

The value is shorter than the initial delay.

Policy

The policy the attribute declares. Its MaxAttempts is how many times the job is retried: 0 for [RetryPolicy(0)], the number of delays for the explicit form.

public RetryPolicy Policy { get; }

Property Value

RetryPolicy

See Also