mercredi 15 mai 2019

Hangfire retry pattern

It is possible to retry task till condition is not completed ? e.g.

internal class Program
{
    private static void Main(string[] args)
    {
        var user = new User();
        var jobs = new Jobs();

        Hangfire.BackgroundJob.Enqueue(() => jobs.SendNotification(user));
    }
}


public class Jobs
{
    Rules rules = new Rules();

    public void SendNotification(User user)
    {
        if (rules.Rule1() && rules.Rule2())
        {
            // send notification
            return;
        }

        // somehow retry the execution of this method (throw exception does not seem right)
    }
}

public class Rules
{
    public bool Rule1() { return true; }

    public bool Rule2() { return true; }
}

public class User { }

I know that it is possible to retry execution of method by throwing exception but that does not seem right, since I know that recovering from exception is rather costly, and it will mark job as failed in hangfire admin interface which is not true.

I could write retry pattern myself, but I like the way hangfire is saving all the information related to background job processing to the persistent storage (SQL in my case), no data is kept in a process’ memory. So I assume it can recover the queue from storage even after server was shut down and continue processing.

Aucun commentaire:

Enregistrer un commentaire