rails/activejob/test/jobs/after_discard_retry_job.rb
Rob Cardy 659d41110f
This method lets job authors define a block which will be run when a job is about to be discarded.
This has utility for gems/modules included on jobs, which can tie into this behaviour and run something when a job fails.
after_discard respects the existing retry behaviour, but will run even if a retried exception is handled in a block.
2023-05-08 14:28:42 -04:00

34 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require_relative "../support/job_buffer"
require "active_support/core_ext/integer/inflections"
class AfterDiscardRetryJob < ActiveJob::Base
class UnhandledError < StandardError; end
class DefaultsError < StandardError; end
class CustomCatchError < StandardError; end
class DiscardableError < StandardError; end
class CustomDiscardableError < StandardError; end
class AfterDiscardError < StandardError; end
class ChildAfterDiscardError < AfterDiscardError; end
retry_on DefaultsError
retry_on(CustomCatchError) { |job, error| JobBuffer.add("Dealt with a job that failed to retry in a custom way after #{job.arguments.second} attempts. Message: #{error.message}") }
retry_on(AfterDiscardError)
retry_on(ChildAfterDiscardError)
discard_on DiscardableError
discard_on(CustomDiscardableError) { |_job, error| JobBuffer.add("Dealt with a job that was discarded in a custom way. Message: #{error.message}") }
after_discard { |_job, error| JobBuffer.add("Ran after_discard for job. Message: #{error.message}") }
def perform(raising, attempts)
if executions < attempts
JobBuffer.add("Raised #{raising} for the #{executions.ordinalize} time")
raise raising.constantize
else
JobBuffer.add("Successfully completed job")
end
end
end