Skip to content

Commit

Permalink
Use the job id as the lock value, fix test to work with that assumption
Browse files Browse the repository at this point in the history
  • Loading branch information
Sutto committed Sep 5, 2013
1 parent a897d97 commit 11d7127
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/sidekiq-middleware/client/unique_jobs.rb
Expand Up @@ -12,6 +12,7 @@ def call(worker_class, item, queue)

if enabled
unique, payload = false, item.clone.slice(*%w(class queue args at))
job_id = item['jid']

# Enabled unique scheduled
if enabled == :all && payload.has_key?('at')
Expand All @@ -26,11 +27,12 @@ def call(worker_class, item, queue)
Sidekiq.redis do |conn|
conn.watch(payload_hash)

if conn.get(payload_hash)
locked_job_id = conn.get(payload_hash)
if locked_job_id && locked_job_id != job_id
conn.unwatch
else
unique = conn.multi do
conn.setex(payload_hash, expiration, 1)
conn.setex(payload_hash, expiration, job_id)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/test_unique_jobs.rb
Expand Up @@ -128,7 +128,9 @@ def perform(id, unlock)
it 'does not duplicate messages with enabled unique option and custom unique lock key' do
5.times { CustomUniqueWorker.perform_async('args', false) }
assert_equal 1, Sidekiq.redis { |c| c.llen('queue:custom_unique_queue') }
assert_equal 1, Sidekiq.redis { |c| c.get('custom:unique:lock:args').to_i }
job = Sidekiq.load_json Sidekiq.redis { |c| c.lpop('queue:custom_unique_queue') }
assert job['jid']
assert_equal job['jid'], Sidekiq.redis { |c| c.get('custom:unique:lock:args') }
end

it 'does not allow the job to be duplicated when processing job with manual option' do
Expand Down

0 comments on commit 11d7127

Please sign in to comment.