Failures, retries and stopping
Retries
Section titled “Retries”A job fails when Perform throws, or when it runs past an explicit timeout. MassiveJobs then publishes it again, delayed:
the broker holds it until its retry time, and any app’s worker runs it then. The delays grow with each retry, like
Sidekiq’s: retries⁴ + 15 s, plus some randomness.
| Retry | About |
|---|---|
| 1st | 15 to 45 seconds |
| 5th | 6 minutes |
| 10th | 2 hours |
| 20th | 1.5 days |
| 25th | 4 days |
With the default MaxRetries of 25 a job runs at most 26 times, over about three weeks, before it goes to the failed queue.
Set MaxRetries = 0 to send failed jobs there at once. The app that runs the job decides.
Retries of one job
Section titled “Retries of one job”[JobRetry] on a job class sets its own retries, and optionally its own delays, in place of MaxRetries and the delays
above:
[JobRetry(3, new[] { 10, 60, 300 })] // up to 3 retries: after 10 seconds, a minute, then 5 minutespublic class ExportInvoice(IExporter exporter) : JobAsync<ExportInvoice, int>{ public override Task Perform(int invoiceId, CancellationToken cancellationToken) => exporter.ExportAsync(invoiceId, cancellationToken);}
[JobRetry(0)] // no retries: a failure goes to the failed queue at oncepublic class SendSms(ISmsSender sms) : JobAsync<SendSms, string>{ public override Task Perform(string text, CancellationToken cancellationToken) => sms.SendAsync(text, cancellationToken);}- The delays are in seconds, one per retry; the last one repeats for the retries beyond the list. Without them, the growing delays above apply.
- Plain job classes registered with
WithJob<T>()can have it too, and derived job classes inherit it. - The app that runs the job decides, as with
MaxRetries: apps that share a job register the same class, so they agree. - Negative retries or delays fail at startup.
The job’s last error and its retry count travel with it. MassiveJobs logs every failure at error level, with the exception and the retry count.
The failed queue
Section titled “The failed queue”Jobs that ran out of retries end up in the failed queue ({prefix}massivejobs.failed), together with messages that
couldn’t be read as a job, jobs no app registered, and messages RabbitMQ gave up on. Nothing consumes it. Inject
IFailedJobs to look at it and act on it, e.g. from an admin endpoint:
var failed = app.MapGroup("/admin/failed-jobs");
failed.MapGet("/", async (IFailedJobs jobs) => (await jobs.PeekAsync(50)) .Select(f => new { f.JobTag, f.Error, f.OriginalQueue, Retries = f.Job?.Retries }));
failed.MapGet("/count", (IFailedJobs jobs) => jobs.CountAsync());
// once the bug is fixedfailed.MapPost("/requeue", (IFailedJobs jobs, string? tag) => jobs.RequeueAsync(f => tag == null || f.JobTag == tag));
failed.MapDelete("/", (IFailedJobs jobs, string tag) => jobs.PurgeAsync(f => f.JobTag == tag));| Method | |
|---|---|
CountAsync() |
How many messages are there (approximate while others look at them). |
PeekAsync(maxCount) |
The oldest ones, with the job, its arguments, the error and the queue it came from. They stay. |
RequeueAsync(filter) |
Publishes them again and removes them. A job starts over: it runs now, with a fresh retry count. |
PurgeAsync(filter) |
Deletes them. |
A message the app can’t read (a tag it doesn’t register, a corrupt body) goes back unchanged to the queue it came from, if that’s known. A requeued run of a periodic job runs once, as an ordinary job.
A queue has no random access: each call reads the messages from the front, and holds the ones it reads until it returns. Meanwhile, other calls don’t see them.
Unknown jobs
Section titled “Unknown jobs”During a rolling deployment the new version of an app can publish a job that the old version, still running, doesn’t
register. A worker that gets a tag it doesn’t know delays the message (5 seconds, growing to a minute) so that an app
that knows the job gets it, and after UnknownJobTimeout (30 minutes) moves it to the failed queue.
So deploy apps that run a new job before, or together with, apps that publish it. Apps whose workers share queues should register the same jobs.
Stopping
Section titled “Stopping”When the host stops, the workers stop taking jobs at once and let the running ones finish:
- The running jobs get
ShutdownTimeout(20 s) to finish. They’re acknowledged as usual. - Then their cancellation token is cancelled, and the workers wait at most 5 seconds more.
- A job still running then is left behind. Its message goes back to the queue when the app’s connection closes, so it can run twice.
A job cancelled by the shutdown doesn’t count as failed, whatever it throws: no retry, no failed queue. It runs again from the start, in this app or another.
The host has its own limit, HostOptions.ShutdownTimeout (30 s by default), after which it cancels the stop and with it
the running jobs. Make ShutdownTimeout plus 5 seconds fit in it:
builder.Services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(60));builder.Services.AddMassiveJobs(o => o.ShutdownTimeout = TimeSpan.FromSeconds(45));Messages a worker had received but not started go back to the queue at once. RabbitMQ counts each such return as a delivery: a message returned 20 times goes to the failed queue, from where you can requeue it.