Skip to content

Commit

Permalink
Add a default concurrency key if not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed Nov 9, 2023
1 parent 8f93856 commit 1f79e57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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?

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

0 comments on commit 1f79e57

Please sign in to comment.