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:
| Attribute | Policy |
|---|---|
[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
maxAttemptsint0. Any other count needs a delay.
Exceptions
- ArgumentOutOfRangeException
maxAttemptsis not0.
RetryPolicyAttribute(int, string)
Declares Fixed(int, TimeSpan): the same wait before every retry.
public RetryPolicyAttribute(int maxAttempts, string delay)
Parameters
maxAttemptsintHow many times to retry after the first failure; at least one.
delaystringThe wait before each retry, as an invariant TimeSpan:
"00:05:00"is five minutes.
Exceptions
- ArgumentException
delayis not a TimeSpan.- ArgumentOutOfRangeException
maxAttemptsis less than one, ordelayis 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
maxAttemptsintHow many times to retry after the first failure; at least one.
initialDelaystringThe wait before the first retry, as an invariant TimeSpan.
factordoubleWhat each wait is multiplied by to get the next one; at least
1.
Exceptions
- ArgumentException
initialDelayis not a TimeSpan.- ArgumentOutOfRangeException
maxAttemptsis less than one,initialDelayis negative, orfactoris 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
delaysstring[]The wait before each retry, each an invariant TimeSpan; at least one, none negative.
Exceptions
- ArgumentNullException
delaysis null.- ArgumentException
delaysis 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
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
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; }