I'm looking for a way to handle Rails ActiveJob jobs that reached the max retry attempts.
Here is the snippet of my code:
class SomeJob < ApplicationJob
queue_as :default
retry_on StandardError, wait: :polynomially_longer, attempts: 3
def perform(args)
# Do things here
end
end
In my case, the job tries a couple of things, including connecting to 3rd party APIs. It happens that the API is not available for a couple of times and the max attempts is reached.
In that case, I want to catch this case and perform some additional logic like updating User records and notifying our team.
I'm using SolidQueue, and was thinking about getting the executions
count argument, but again, I need to identify the moment the job has definitely failed.
Any idea of robust a solution to this? It looks like I'm looking for a Dead Letter Queue system.
Other responses and comments are valid solutions. I ended up implementing the following feature from ActiveJob:
class SomeJob < ApplicationJob
queue_as :default
retry_on StandardError, wait: :polynomially_longer, attempts: 3 do |job, error|
fallback(*job.arguments)
end
def perform(args)
# Do things here
end
def fallback(args)
# This will only be executed once, when the max retries is reached
end
end
The *job.arguments
will ensure the fallback
method will get the exact same arguments as the perform
method, with the same order.
I got the inspiration from the examples of retry_on
of the Rails doc: https://edgeapi.rubyonrails.org/classes/ActiveJob/Exceptions/ClassMethods.html#method-i-retry_on
Hope it will help someone.