Retrying queue messages

When you implement a queue processing job you might end up in a situation where you can't process the message right now but you know that you'll soon want to try it again. Such as if you detect that a system that you want to send the message to is down at the moment so you want to try again in X minutes.

Nexus lets you handle that by returning ProcessResults.RetryLater() from a queue job or call batch.RetryLater() in a batch queue job. You specify the amount of time to wait and then Nexus will handle the rest.

Nexus will place the message in a new status called AwaitingRetry and it will sit there until it's time to try agian. At that point the message status is set to Pending again and the processing job will pick it up again.

Max retries

To avoid having messages that end up in an inifinite loop of retries Nexus has a default limit on how many times a message can be retried. The default limit is 10 and once a message has reached that number of retries it will get the status Error and a comment saying Message was retried too many times (10) and has been placed in the Error status. At that point it will not be automatically retried again but you can always manually set it back to Pending which gives the message 10 more tries.

If you want to change the limit of max number of retries you can either set it on all queues like this in your Program.cs:

builder.Services.AddNexusQueues(options =>
{
    options.DefaultMaxRetries = 5;
});

You can also set this per queue using the [Queue] attribute:

[Queue("example", MaxRetrie = 5)]
public class ExampleQueueMessage : IQueueMessage
{

}

Note that you can allow an infinite number of retries by setting -1 as the max.