# A process after reaching max retry

**URL:** <https://discuss.hangfire.io/t/a-process-after-reaching-max-retry/3933>\
**Category:** feature\
**Created:** [October 20, 2017, 6:33pm UTC](https://discuss.hangfire.io/t/a-process-after-reaching-max-retry/3933 "2017-10-20T18:33:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Kwak](https://avatars.discourse-cdn.com/v4/letter/b/da6949/32.png) [@Brian\_Kwak](https://discuss.hangfire.io/u/Brian_Kwak)\
**Post date:** [October 20, 2017, 6:33pm UTC](https://discuss.hangfire.io/t/a-process-after-reaching-max-retry/3933/1 "2017-10-20T18:33:33Z")

</div>

Hi, is there a way to process a job/methods when max automatic retry?  
Let’s say, I have a job to send email to customer after a customer orders, and I would like to cancel the order if the sending a notification email job reaches the max retry and fails.

---

<div class="post-metadata">

**Author:** ![WallePuh](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/wallepuh/32/1132_2.png) [@WallePuh](https://discuss.hangfire.io/u/WallePuh)\
**Post date:** [October 23, 2017, 1:16pm UTC](https://discuss.hangfire.io/t/a-process-after-reaching-max-retry/3933/2 "2017-10-23T13:16:27Z")

</div>

You could create your own retry handler with a `IElectStateFilter` and do your logic in the `OnStateElection` method when `context.CandidateState` is in `FailedState`.  
If you want to keep track on how many tries has been made you can set/get it with `context.SetJobParameter("RetryCount", retryAttempts)` and `retryAttempts = context.GetJobParameter<int?>("RetryCount") ?? 0`.

You can get the idea on how it is implemented in the framework here:

> <https://github.com/HangfireIO/Hangfire/blob/70a757d6d493ec47c5bb3b8a7be8b875421abd43/src/Hangfire.Core/AutomaticRetryAttribute.cs>
