Class RetryPolicy
- Namespace
- Quartz
- Assembly
- Quartz.dll
How often, and how far apart, the scheduler re-fires a trigger whose job has failed.
public sealed class RetryPolicy : IEquatable<RetryPolicy>
- Inheritance
-
RetryPolicy
- Implements
- Inherited Members
Remarks
A policy is a value: two policies of the same shape that would produce the same waits are equal. There is no public constructor — Fixed(int, TimeSpan), Exponential(int, TimeSpan, double, TimeSpan?) and Explicit(params IReadOnlyList<TimeSpan>) are the only ways to make one, so a policy that could not be honoured (no attempts, a negative wait, a backoff that shrinks) cannot be built at all.
MaxAttempts counts retries after the first failure, not fires: a trigger
carrying Fixed(2, …) whose job keeps throwing runs three times in total and is then back
on its ordinary schedule.
A retry never displaces the trigger's own next occurrence. The scheduler drops a retry that would land at (or within a second of) the regular next fire time and lets the schedule win, so a policy whose waits are longer than the gap between occurrences quietly does nothing.
Properties
BackoffFactor
What each wait is multiplied by to get the next one. 1 means a fixed wait.
public double BackoffFactor { get; }
Property Value
Remarks
Persisted with the round-trip ("R") format and the invariant culture, so a policy
written on one machine reads back bit for bit on another.
Delays
The explicit table of waits, longest-lived entry last; empty when the waits are computed from InitialDelay and BackoffFactor.
public ImmutableArray<TimeSpan> Delays { get; }
Property Value
Remarks
A table shorter than the number of attempts cannot happen — MaxAttempts is its length — but the last entry repeats for any attempt beyond it, so the property is safe to index through DelayFor(int) alone.
InitialDelay
The wait before the first retry. For an Explicit(params IReadOnlyList<TimeSpan>) policy this is the first entry of Delays.
public TimeSpan InitialDelay { get; }
Property Value
MaxAttempts
How many times the scheduler retries after the first failure. Always at least one.
public int MaxAttempts { get; }
Property Value
Remarks
This is the authoritative count: an Explicit(params IReadOnlyList<TimeSpan>) policy's is the length of its delay table, and once the attempts are spent the trigger returns to its ordinary schedule rather than going into an error state.
MaxDelay
The ceiling every computed wait is clamped to, or null when the backoff grows unbounded.
public TimeSpan? MaxDelay { get; }
Property Value
Methods
DelayFor(int)
The wait before the given retry.
public TimeSpan DelayFor(int attempt)
Parameters
attemptintWhich retry to compute the wait for, counting from one:
1is the wait after the first failure.
Returns
- TimeSpan
The wait, clamped to MaxDelay when there is one. An attempt beyond MaxAttempts answers as the last one does rather than throwing, because the decision to stop retrying belongs to the scheduler and not to the arithmetic.
Remarks
A wait too long to be represented is not an error. The arithmetic saturates, and the scheduler treats a retry it cannot express an instant for the way it treats one that would land on top of the next occurrence: there is no room for it, so the occurrence settles and the trigger keeps its ordinary schedule. Capping the waits when a policy is built or parsed instead would refuse a stored form that ToStoredString() had just written, and would refuse a policy over attempts no trigger will reach.
Exceptions
- ArgumentOutOfRangeException
attemptis less than one.
Equals(RetryPolicy?)
Whether the other policy has the same shape and would produce exactly the same waits, the same number of times.
public bool Equals(RetryPolicy? other)
Parameters
otherRetryPolicyThe policy to compare with.
Returns
Remarks
Written out by hand rather than left to a record: the delay table is a collection, and
reference equality on it would make two policies built from the same numbers unequal — which
the store contract tests compare through. The backoff factor is compared bit for bit, which
is what round-tripping it through the "R" format guarantees and what "the same policy
came back out of the column" means.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Explicit(params IReadOnlyList<TimeSpan>)
A policy that waits the given amounts, in order. MaxAttempts is the number of waits given.
public static RetryPolicy Explicit(params IReadOnlyList<TimeSpan> delays)
Parameters
delaysIReadOnlyList<TimeSpan>The wait before each retry, none of them negative and at least one of them present.
Returns
Exceptions
- ArgumentNullException
delaysis null.- ArgumentException
delaysis empty, holds a negative wait, or is long enough that the stored form would not fit the triggers table's 250-character retry policy column.
Exponential(int, TimeSpan, double, TimeSpan?)
A policy whose wait grows by a constant factor with every retry.
public static RetryPolicy Exponential(int maxAttempts, TimeSpan initialDelay, double factor = 2, TimeSpan? maxDelay = null)
Parameters
maxAttemptsintHow many times to retry after the first failure; at least one.
initialDelayTimeSpanThe wait before the first retry; not negative.
factordoubleWhat each wait is multiplied by to get the next one. At least
1, which makes the policy wait the same amount every time; a shrinking backoff is not a backoff.maxDelayTimeSpan?A ceiling for the computed waits, or null to let them grow unbounded. Not shorter than
initialDelay, which a ceiling below the first wait would silently undo.
Returns
Remarks
A factor of 1 is legal and produces the same waits as Fixed(int, TimeSpan), but it is
a different value: it says the trigger's author chose a backoff and set its rate to one,
which is a thing to change rather than a fixed wait spelled the long way.
Exceptions
- ArgumentOutOfRangeException
maxAttemptsis less than one,initialDelayis negative,factoris less than one or is not a number, ormaxDelayis shorter thaninitialDelay.
Fixed(int, TimeSpan)
A policy that waits the same amount of time before every retry.
public static RetryPolicy Fixed(int maxAttempts, TimeSpan delay)
Parameters
maxAttemptsintHow many times to retry after the first failure; at least one.
delayTimeSpanThe wait before each retry; not negative.
Returns
Exceptions
- ArgumentOutOfRangeException
maxAttemptsis less than one, ordelayis negative.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Parse(string)
Rebuilds a policy from the string ToStoredString() produced.
public static RetryPolicy Parse(string value)
Parameters
valuestringThe stored form.
Returns
- RetryPolicy
The policy.
Exceptions
- ArgumentNullException
valueis null.- FormatException
valueis not a stored retry policy.
ToStoredString()
The policy as the triggers table holds it: a compact, culture-invariant string that starts with a marker naming the policy's shape.
public string ToStoredString()
Returns
- string
The stored form. Two policies that are equal produce the same string, and Parse(string) turns that string back into an equal policy.
ToString()
The policy as the triggers table holds it, which is the whole of its state.
public override string ToString()
Returns
TryParse(string?, out RetryPolicy?)
Rebuilds a policy from the string ToStoredString() produced, answering whether the string was one.
public static bool TryParse(string? value, out RetryPolicy? policy)
Parameters
valuestringThe stored form, which may be null or blank.
policyRetryPolicyThe policy, or null when there was not one.
Returns
Remarks
A null or blank string answers false rather than throwing: that is what a trigger row with no retry policy reads as.
Operators
operator ==(RetryPolicy?, RetryPolicy?)
Whether two policies are the same value.
public static bool operator ==(RetryPolicy? left, RetryPolicy? right)
Parameters
leftRetryPolicyThe first policy.
rightRetryPolicyThe second policy.
Returns
operator !=(RetryPolicy?, RetryPolicy?)
Whether two policies are different values.
public static bool operator !=(RetryPolicy? left, RetryPolicy? right)
Parameters
leftRetryPolicyThe first policy.
rightRetryPolicyThe second policy.