Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "NoMethodError: private method `_good_job_concurrency_key' if key is nil" #836

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/good_job/active_job_extensions/concurrency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions spec/lib/good_job/active_job_extensions/concurrency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down