[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 ā¦
}
}