ActiveJob is used to enqueue and run the tasks in the background. But a job can fail because of many reasons.
Before
To manage job failures, ActiveJob provides methods like retry_on
to reschedule job for re-execution
and discard_on
to discard the job with no attempts to retry , if the exception is raised.
While these methods provide a way to handle errors and retries, ActiveJob does not provide any method to excute custom logic to handle job failures or to perform cleanup actions after a job is discarded.
class MyJob < ApplicationJob
discard_on StandardError
def perform
raise StandardError, "An error occurred"
end
end
After
Rails 7.1 adds after_discard method to ActiveJob to run a callback when a job is about to be discarded.
The after_discard
callback allows us to define a block of code that runs after a job is discarded.
This callback gives us access to both the job instance and the exception that caused the job to be discarded.
after_discard do |job, exception|
# add additional logic for error handling.
end
class MyJob < ApplicationJob
discard_on StandardError
after_discard :log_discard_info
def perform
raise StandardError, "An error occurred"
end
private
def log_discard_info(job, exception)
Rails.logger.info("Job #{job.class} was discarded due to: #{exception.message}")
# Additional cleanup logic can be added here
end
end
after_discard
is called in any situation where the job is about to be discarded due to a failure. Those cases are:
-
When exception is raised without
retry_on
ordiscard_on
. -
When
discard_on
is used to handle an exception, with or without a block. -
When
retry_on
is used and all retries are exhausted, with or without a block. -
ActiveJob will run all
after_discard
blocks even if some of them raise exceptions. -
Only the last exception raised by the
after_discard
block will be reported and earlier exceptions are suppressed.
The after_discard
method enables us to handle failures more effectively by performing tasks like logging detailed error information, notifying external systems, or executing cleanups.