Hangfire retry with custom parameter

aspnetcore
sql-server
recurring
Tags: #<Tag:0x00007f69fd3b5c68> #<Tag:0x00007f69fd3b5998> #<Tag:0x00007f69fd3b57e0>

#1

Hi,

I have a ASP.NET Core 2.1 app which implements hangfire to schedule recurring jobs which fires off bot messages to a collection of users in our db. Now I would like to be able to pass back a response with id’s of users who were not successfully messaged and do hangfire retries for only those users. Is this possible to implement, or am I better off not throwing any exception and reiterating the shrinking list with my own logic?

I have read the documentation - only thing I saw about passing arguments to your jobs is this: http://docs.hangfire.io/en/latest/background-methods/passing-arguments.html

But I am not sure how this would apply to my AddOrUpdate implementation. I’ve also looked at injecting PerformContext but that seems useful only to set existing parameters.

Below is what my job currently looks like. 'UserId ’ is where I would pass the list of id’s, which initially will be null, prompting my business logic to fetch all users. Then if exception is reached the job is retried - but with the list of failed UserIds passed on through hangfire.

    // At 15:00 every Tuesday
[Interval("0 15 * * 2")]
public class SendMessageJob : IJob
{
    private readonly IMediator _mediator;

    public SendMessageJob (IMediator mediator)
    {
        _mediator = mediator;
    }

    public async Task HandleAsync()
    {
        var res = await _mediator.Send(new SendMessageRequest { MessageId = 2, UserIds = null });
        if (res.Failed)
        {
            throw new Exception("These users could not be questioned: " + res.FailedUsers);
        }               
    }
}