From c5fb874891dbcd0c4f6f42f326885badc26818e8 Mon Sep 17 00:00:00 2001 From: "Ben Sheldon [he/him]" Date: Wed, 8 Feb 2023 14:32:33 -0800 Subject: [PATCH] Fix "NoMethodError: private method `_good_job_concurrency_key' if key is nil" --- .../active_job_extensions/concurrency.rb | 24 +++++++++---------- .../active_job_extensions/concurrency_spec.rb | 21 ++++++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/good_job/active_job_extensions/concurrency.rb b/lib/good_job/active_job_extensions/concurrency.rb index 109acc90e..54179c07d 100644 --- a/lib/good_job/active_job_extensions/concurrency.rb +++ b/lib/good_job/active_job_extensions/concurrency.rb @@ -86,6 +86,18 @@ def good_job_concurrency_key @good_job_concurrency_key || _good_job_concurrency_key end + # Generates the concurrency key from the configuration + # @return [Object] concurrency key + def _good_job_concurrency_key + key = self.class.good_job_concurrency_config[:key] + return if key.blank? + + key = key.respond_to?(:call) ? instance_exec(&key) : key + raise TypeError, "Concurrency key must be a String; was a #{key.class}" unless VALID_TYPES.any? { |type| key.is_a?(type) } + + key + end + private def good_job_enqueue_concurrency_check(job, on_abort:, on_enqueue:) @@ -129,18 +141,6 @@ def good_job_enqueue_concurrency_check(job, on_abort:, on_enqueue:) end end end - - # Generates the concurrency key from the configuration - # @return [Object] concurrency key - def _good_job_concurrency_key - key = self.class.good_job_concurrency_config[:key] - return if key.blank? - - key = key.respond_to?(:call) ? instance_exec(&key) : key - raise TypeError, "Concurrency key must be a String; was a #{key.class}" unless VALID_TYPES.any? { |type| key.is_a?(type) } - - key - end end end end diff --git a/spec/lib/good_job/active_job_extensions/concurrency_spec.rb b/spec/lib/good_job/active_job_extensions/concurrency_spec.rb index abfd4ded0..3526c7b27 100644 --- a/spec/lib/good_job/active_job_extensions/concurrency_spec.rb +++ b/spec/lib/good_job/active_job_extensions/concurrency_spec.rb @@ -16,6 +16,27 @@ def perform(name:) end) end + describe 'when extension is only included but not configured' do + it 'does not limit concurrency' do + expect do + TestJob.perform_later(name: "Alice") + GoodJob.perform_inline + end.not_to raise_error + end + end + + describe 'when concurrency key is nil' do + it 'does not limit concurrency' do + TestJob.good_job_control_concurrency_with( + total_limit: -> { 1 }, + key: -> {} + ) + + expect(TestJob.perform_later(name: "Alice")).to be_present + expect(TestJob.perform_later(name: "Alice")).to be_present + end + end + describe '.good_job_control_concurrency_with' do describe 'total_limit:', skip_rails_5: true do before do