[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Fail)] is not working.
On exception in my job it goes to schedule state and then Retry attempts happens !!!
I tried wiriting this attribute on controller and business class methods and thrown exception from data access layer method but in state table it shows retry attempts.
I have the same problem. I get a warning like “Error occurred during execution of ‘Worker #9’ component. Execution will be retried (attempt 1 of 2147483647) in 00:00:00 seconds.”
I was able to resolve my issue. I had a custom Filter; that was throwing an exception. This apparently aborted the normal processing of jobs, and caused the ‘attempt 1 of 2147483647’ problem, etc…
I’m using asp.net core and I have a simple fire and forget background job. Using a global filter was not an option for me. For some reason the AutomaticRetryAttribute was being ignored. It turned out the way I was adding the job was the key to my solution. I had a similar code in my app that was causing the issue:
In my IMyJobService implementation I had the following code:
[AutomaticRetry(Attempts = 0)]
public void DoWork()
{
// I'm working hard here
}
The solution that I came up with was:
public MyTestController
{
private readonly IMyJobService _myJobService;
public MyTestClass(IMyJobService myJobService)
{
_myJobService = myJobService;
}
public ActionResult Work()
{
BackgroundJob.Enqueue(() => _myJobService.DoWork());
return Ok();
}
}
Instead of relying on BackgroundJob.Enqueue<T> to inject my IMyJobService implementation I do it myself. That’s basically it. I hope this will help someone.
Most likely it’s not a ‘bug’ with attribute based filters causing this. If you’re using a DI container with an interface (like the following) then you must put the hangfire attributes on the interface and not the implementation.
Ironically, it works for me if I decorate also the implemented method
,
public class GenerateSurveyJob : IGenerateSurveyJob
{
[AutomaticRetry(Attempts = 1, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public async Task Generate(long orderId)
{
… lots of stuff …
}
}