From a6285f6566cefe5c79e797a5e19585d6b5b47608 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 27 Feb 2020 16:41:35 +0100 Subject: [PATCH] Refactoring: Extract ActiveJob logic into concern to prevent/avoid fat class. --- app/jobs/application_job.rb | 16 ++------------ ...has_delayed_job_monitoring_compatibilty.rb | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 app/jobs/application_job/has_delayed_job_monitoring_compatibilty.rb diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index f1d7c0233..9c8c0ec7d 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -1,21 +1,9 @@ class ApplicationJob < ActiveJob::Base + include ApplicationJob::HasDelayedJobMonitoringCompatibilty + # Automatically retry jobs that encountered a deadlock # retry_on ActiveRecord::Deadlocked # Most jobs are safe to ignore if the underlying records are no longer available # discard_on ActiveJob::DeserializationError - - # We (currently) rely on Delayed::Job#attempts to check for stuck backends - # e.g. in the MonitoringController. - # This is a workaround to sync ActiveJob#executions to Delayed::Job#attempts - # until we resolve this dependency. - after_enqueue do |job| - # skip update of `attempts` attribute if job wasn't queued because of ActiveJobLock - #(another job with same lock key got queued before this job could be retried) - next if job.provider_job_id.blank? - - # update the column right away without loading Delayed::Job record - # see: https://stackoverflow.com/a/34264580 - Delayed::Job.where(id: job.provider_job_id).update_all(attempts: job.executions) # rubocop:disable Rails/SkipsModelValidations - end end diff --git a/app/jobs/application_job/has_delayed_job_monitoring_compatibilty.rb b/app/jobs/application_job/has_delayed_job_monitoring_compatibilty.rb new file mode 100644 index 000000000..019cd0087 --- /dev/null +++ b/app/jobs/application_job/has_delayed_job_monitoring_compatibilty.rb @@ -0,0 +1,21 @@ +class ApplicationJob + module HasDelayedJobMonitoringCompatibilty + extend ActiveSupport::Concern + + included do + # We (currently) rely on Delayed::Job#attempts to check for stuck backends + # e.g. in the MonitoringController. + # This is a workaround to sync ActiveJob#executions to Delayed::Job#attempts + # until we resolve this dependency. + after_enqueue do |job| + # skip update of `attempts` attribute if job wasn't queued because of ActiveJobLock + #(another job with same lock key got queued before this job could be retried) + next if job.provider_job_id.blank? + + # update the column right away without loading Delayed::Job record + # see: https://stackoverflow.com/a/34264580 + Delayed::Job.where(id: job.provider_job_id).update_all(attempts: job.executions) # rubocop:disable Rails/SkipsModelValidations + end + end + end +end