From c6a17c90feeedb934bac7804e691e48a118a6d86 Mon Sep 17 00:00:00 2001 From: Karol Bucek Date: Fri, 9 Sep 2016 15:50:32 +0200 Subject: [PATCH] can_enqueue? propagation detection that does not depend on Rails (#1672) --- app/jobs/agent_propagate_job.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/jobs/agent_propagate_job.rb b/app/jobs/agent_propagate_job.rb index 5d0ceaf8..e66079c1 100644 --- a/app/jobs/agent_propagate_job.rb +++ b/app/jobs/agent_propagate_job.rb @@ -6,14 +6,15 @@ class AgentPropagateJob < ActiveJob::Base end def self.can_enqueue? - if Rails.configuration.active_job.queue_adapter == :delayed_job && - Delayed::Job.where(failed_at: nil, queue: 'propagation').count > 0 - return false - elsif Rails.configuration.active_job.queue_adapter == :resque && - (Resque.size('propagation') > 0 || - Resque.workers.select { |w| w.job && w.job['queue'] && w.job['queue']['propagation'] }.count > 0) - return false + case queue_adapter.name # not using class since it would load adapter dependent gems + when 'ActiveJob::QueueAdapters::DelayedJobAdapter' + return Delayed::Job.where(failed_at: nil, queue: 'propagation').count == 0 + when 'ActiveJob::QueueAdapters::ResqueAdapter' + return Resque.size('propagation') == 0 && + Resque.workers.select { |w| w.job && w.job['queue'] && w.job['queue']['propagation'] }.count == 0 + else + raise NotImplementedError, "unsupported adapter: #{queue_adapter}" end - true end + end