Failures & Retries
When a Run Fails
A run fails when the handler throws, or when it stops at its timeout (job.CancellationToken was cancelled). The job is then retried, up to its maximum number of retries; after the last one fails, it ends as Failed.
The maximum comes from the call (maxNumberOfRetries:), the handler's [JobMasterMaxNumberOfRetries], or the cluster's DefaultMaxRetryCount (3 if the cluster doesn't set it).
In the handler, job.NumberOfFailures tells a retry from a first attempt: it's 0 on the first run, 1 on the first retry, and so on.
JobMaster runs every job at least once: after a retry, or when a worker stops in the middle of a run, the same job runs again. Check whether the work is already done before doing it again.
Retry Delays
The wait before each retry is set per cluster with RetryDelays. By default it's 30 seconds, 2 minutes, 10 minutes, 30 minutes, then 1 hour for every later retry.
config.RetryDelays(TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(30));
The first delay is before the first retry, and so on; the last one repeats for every later retry. Each delay must be at least 30 seconds.
Intercepting Retries
A retry interceptor decides, after each failed run that still has retries left, how the job continues.
It can:
- retry after a different delay than
RetryDelays, shorter or longer (at least 30 seconds); - stop retrying early, so the job ends as
Failednow; - keep the
RetryDelaysonly under certain conditions, for example only for some exceptions or jobs.
It can't increase the number of retries: the job's maximum still applies, and it isn't called after the last retry.
It returns one of three decisions:
| Decision | Effect |
|---|---|
JobRetryDecision.UseRetryDelays() | The default flow: retry after the cluster's RetryDelays, exactly as when there's no interceptor |
JobRetryDecision.RetryAfter(delay) | Retry after this delay (at least 30 seconds) |
JobRetryDecision.StopEarly("reason") | Don't retry: the job ends as Failed now. The reason is shown on the failed run in the dashboard. |
It gets the job (ctx.Job, with its metadata and message data), the number of failures so far (ctx.NumberOfFailures, this one included) and the exception (ctx.Exception; a TimeoutException when the run timed out).
As a Function
For a quick rule with no dependencies:
config.InterceptRetry(ctx => ctx.Exception switch
{
ArgumentException => JobRetryDecision.StopEarly("Invalid input, retrying won't help."),
HttpRequestException => JobRetryDecision.RetryAfter(TimeSpan.FromMinutes(1)),
_ => JobRetryDecision.UseRetryDelays()
});
As a Class
For a rule that needs services or I/O, implement IJobMasterRetryInterceptor. It's created from DI in a new scope for each failed run, so scoped services work:
public sealed class PaymentRetryInterceptor(IPaymentGateway gateway) : IJobMasterRetryInterceptor
{
public async Task<JobRetryDecision> DecideAsync(JobRetryContext ctx, CancellationToken cancellationToken)
{
if (await gateway.IsDownAsync(cancellationToken))
{
return JobRetryDecision.RetryAfter(TimeSpan.FromMinutes(15));
}
return JobRetryDecision.UseRetryDelays();
}
}
config.InterceptRetry<PaymentRetryInterceptor>();
Things to Know
- One interceptor per cluster; it's called for every job of that cluster.
- It runs on the worker that ran the job, so register it in every app that runs jobs.
- Keep it fast: if it throws, or doesn't answer within 10 seconds (its
cancellationTokenis cancelled then), the job is retried with theRetryDelays.