Task after maximum retires is reaches

recurring
Tags: #<Tag:0x00007f499acb1ae8>

#1

Hey guys,

I was wondering, has anyone implemented anything or precess that get’s triggered when HangFire reached the maximum retries. For example, let’s say hangfire has reached the maximum 10 retries and the job still fails. I’d like to send an email to myself about it.


#2

I have implemented this by creating a custom job filter which does the following:

 public void OnStateElection(ElectStateContext context)
        {
            var failedState = context.CandidateState as FailedState;
            if (failedState != null)
            {
                var retryAttempt = context.GetJobParameter<int>("RetryCount") + 1;
                var maxAttempts = _schedulerConfiguration.MaxNumberOfRetries;
                if (retryAttempt == maxAttempts) {
                     // Send an email
                }
            }
        }

#3

Thanks for this.

I’m using a webapi, do I put this on the startup where I do the Hangfire configuration ?