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

Use the job class as the default concurrency key if none is provided #1145

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ class MyJob < ApplicationJob

# A unique key to be globally locked against.
# Can be String or Lambda/Proc that is invoked in the context of the job.
#
# If a key is not provided GoodJob will create one for you. This key will be
# composed of the jobs class, the queue, and all passed arguments.
# `MyJob.set(queue: "special_queue").perform_later("Bob")` => 'MyJob-special_queue-["Bob"]'
# If you provide a custom concurrency key (for example, if one of your arguments
# is transient) make sure that it sufficiently unique across jobs and queues
# by adding the job class or queue to the key yourself, if needed.
#
# Note: Arguments passed to #perform_later can be accessed through ActiveJob's `arguments` method
# which is an array containing positional arguments and, optionally, a kwarg hash.
key: -> { "MyJob-#{arguments.first}-#{arguments.last[:version]}" } # MyJob.perform_later("Alice", version: 'v2') => "MyJob-Alice-v2"
Expand Down
2 changes: 1 addition & 1 deletion lib/good_job/active_job_extensions/concurrency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def good_job_concurrency_key
# @return [Object] concurrency key
def _good_job_concurrency_key
key = self.class.good_job_concurrency_config[:key]
return if key.blank?
return "#{self.class.name}-#{queue_name}-#{ActiveJob::Arguments.serialize(arguments)}" if key.blank?
Earlopain marked this conversation as resolved.
Show resolved Hide resolved

key = instance_exec(&key) if key.respond_to?(:call)
raise TypeError, "Concurrency key must be a String; was a #{key.class}" unless VALID_TYPES.any? { |type| key.is_a?(type) }
Expand Down
27 changes: 27 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 @@ -194,6 +194,33 @@ def perform
end
end

context 'when no key is specified' do
before do
stub_const 'TestJob', (Class.new(ActiveJob::Base) do
include GoodJob::ActiveJobExtensions::Concurrency

def perform(name)
end
end)
end

it 'calculates a default concurrency key' do
job = TestJob.perform_later("Alice")
expect(job.good_job_concurrency_key).to eq('TestJob-default-["Alice"]')
end

it 'uses the overwritten queue name in the key' do
job = TestJob.set(queue: "different").perform_later("Alice")
expect(job.good_job_concurrency_key).to eq('TestJob-different-["Alice"]')
end

it 'works with complex parameters' do
execution = GoodJob::Execution.create(active_job_id: SecureRandom.uuid, queue_name: "default")
job = TestJob.perform_later(execution)
expect(job.good_job_concurrency_key).to eq("TestJob-default-[{\"_aj_globalid\"=>\"#{execution.to_global_id}\"}]")
end
end

describe '#perform_later' do
before do
stub_const 'TestJob', (Class.new(ActiveJob::Base) do
Expand Down