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

Calculate worker's unique args when a proc or a symbol is specified #143

Merged
merged 2 commits into from
Oct 31, 2015
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
2 changes: 1 addition & 1 deletion lib/sidekiq_unique_jobs/unique_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(job)
@item = job
@worker_class ||= worker_class_constantize(@item[CLASS_KEY])
@item[UNIQUE_PREFIX_KEY] ||= unique_prefix
@item[UNIQUE_ARGS_KEY] ||= unique_args(@item[ARGS_KEY])
@item[UNIQUE_ARGS_KEY] = unique_args(@item[ARGS_KEY]) # SIC! Calculate unique_args unconditionally
@item[UNIQUE_DIGEST_KEY] ||= unique_digest
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/custom_queue_job_with_filter_method.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CustomQueueJobWithFilterMethod < CustomQueueJob
sidekiq_options unique: :until_executed, unique_args: :args_filter

def self.args_filter(*args)
def self.args_filter(args)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I obviously forgot to remove splatting of arguments in a few places!

args.first
end
end
6 changes: 3 additions & 3 deletions spec/jobs/custom_queue_job_with_filter_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class CustomQueueJobWithFilterProc < CustomQueueJob
# slightly contrived example of munging args to the
# worker and removing a random bit.
sidekiq_options unique: :until_timeout,
unique_args: (lambda do |*args|
unique_args: (lambda do |args|
options = args.extract_options!
options.delete(:random)
[args, options]
options.delete('random')
args + [options]
end)
end
43 changes: 43 additions & 0 deletions spec/lib/sidekiq_unique_jobs/unique_args_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@
let(:item) { { 'class' => 'UntilExecutedJob', 'queue' => 'myqueue', 'args' => [[1, 2]] } }
subject { described_class.new(item) }

describe '#unique_digest' do
let(:item) { item_options.merge('args' => [1, 2, 'type' => 'it'] ) }

shared_examples 'unique digest' do
context 'given another item' do
let(:another_subject) { described_class.new(another_item) }

context 'with the same unique args' do
let(:another_item) { item_options.merge('args' => [1, 2, 'type' => 'it']) }
it 'equals to unique_digest for that item' do
expect(subject.unique_digest).to eq(another_subject.unique_digest)
end
end

context 'with different unique args' do
let(:another_item) { item_options.merge('args' => [1, 3, 'type' => 'that']) }
it 'differs from unique_digest for that item' do
expect(subject.unique_digest).to_not eq(another_subject.unique_digest)
end
end
end
end

context 'when unique_args is a proc' do
let(:item_options) do
{ 'class' => 'UntilExecutedJob', 'queue' => 'myqueue',
'unique_args' => Proc.new { |args| args[1] } }
end


it_behaves_like 'unique digest'
end

context 'when unique_args is a symbol' do
let(:item_options) do
{ 'class' => 'UniqueJobWithFilterMethod', 'queue' => 'myqueue',
'unique_args' => :filtered_args }
end

it_behaves_like 'unique digest'
end
end

describe '#unique_args_enabled?' do
with_default_worker_options(unique: :until_executed, unique_args: ->(args) { args[1]['test'] }) do
with_sidekiq_options_for(UntilExecutedJob, unique_args: :unique_args) do
Expand Down