Make delayed job display_name failsafe

The display_name method is used by delayed job to log information
about a certain job, including failure messages. Whenever a job class
is moved or deleted, the instances still scheduled cannot be
constantized anymore, causing display_name and hence the log method to
raise an exception. In certain cases, e.g. when logging happens in a
rescue block, this may terminate the entire delayed job worker. With
the failsafe method, the worker handles failed jobs gracefully and
continues work, all with appropriate log output.
This commit is contained in:
Pascal Zumkehr 2023-03-02 13:38:38 +01:00
parent e78ed07e00
commit 3d3ed6eeb0
2 changed files with 15 additions and 0 deletions

@ -50,6 +50,8 @@ def perform
private
def log_arguments?
job_data["job_class"].constantize.log_arguments?
rescue NameError
false
end
end
end

@ -30,4 +30,17 @@ class DelayedJobAdapterTest < ActiveSupport::TestCase
assert_equal "HelloJob [#{job_id}] from DelayedJob(default) with arguments: #{arguments}",
job_wrapper.display_name
end
test "shows name for invalid job class" do
job_id = SecureRandom.uuid
job_wrapper = ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper.new(
"job_class" => "NotExistingJob",
"queue_name" => "default",
"job_id" => job_id,
"arguments" => { "some" => { "job" => "arguments" } }
)
assert_equal "NotExistingJob [#{job_id}] from DelayedJob(default)", job_wrapper.display_name
end
end