From b6f277c97b6fd53211f4b5b13ca790961baf09a9 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 17 Apr 2019 14:24:52 +0200 Subject: [PATCH] Follow up - 6839f040e0720576188d2bc33e93670c12334add - Fixed bug: Failed jobs get executed more times than configured. --- .../queue_adapters/delayed_job_adapter.rb | 26 +++++++++++++++++++ test/integration/email_deliver_test.rb | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 lib/core_ext/activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb diff --git a/lib/core_ext/activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb b/lib/core_ext/activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb new file mode 100644 index 000000000..a84339550 --- /dev/null +++ b/lib/core_ext/activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb @@ -0,0 +1,26 @@ +# In previous versions of Zammad we used Delayed::Job exclusively +# for performing background jobs. Delayed::Job was therefore in +# charge of scheduling, retrying and executing background jobs. +# After the (partly) migration to Rails ActiveJob this has changed. +# Now ActiveJob is in charge of scheduling and retrying jobs +# while Delayed::Job is still in charge of executing the jobs (assigned to it). +# That leads to an issue where Delayed::Job now falls back to the default +# of 25 retries for a failed job. +# This is not wanted since retries are handled by ActiveJob. +# Therefore the JobWrapper (the class/handler that gets queued) has to define +# max_attempts to be only one. A failing ActiveJob will now be retried as +# often as configured by ActiveJob and then an exception will be raised by +# ActiveJob with the last error message. This message will be rescued by +# Delayed::Job which will see that there are no more attempts wanted and +# will record and handle it as a failed job. +module ActiveJob + module QueueAdapters + class DelayedJobAdapter + class JobWrapper + def max_attempts + 1 + end + end + end + end +end diff --git a/test/integration/email_deliver_test.rb b/test/integration/email_deliver_test.rb index 2b764d261..1adf43d80 100644 --- a/test/integration/email_deliver_test.rb +++ b/test/integration/email_deliver_test.rb @@ -270,7 +270,7 @@ class EmailDeliverTest < ActiveSupport::TestCase assert_raises(RuntimeError) do Scheduler.worker(true) end - assert(Delayed::Job.where(attempts: 4).exists?) + assert(Delayed::Job.none?) ticket1.reload article2_lookup = Ticket::Article.find(article2.id)