1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Fix race condition in unique job lookup, #87

This commit is contained in:
Mike Perham 2012-03-22 20:43:14 -07:00
parent 6ff0cff35e
commit db4198142a
2 changed files with 17 additions and 5 deletions

View file

@ -9,13 +9,23 @@ module Sidekiq
def call(item, queue)
payload_hash = Digest::MD5.hexdigest(MultiJson.encode(item))
unique = false
Sidekiq.redis do |conn|
return if conn.get(payload_hash)
conn.setex(payload_hash, HASH_KEY_EXPIRATION, 1)
conn.watch(payload_hash)
if conn.get(payload_hash)
conn.unwatch
else
unique = conn.multi do
conn.setex(payload_hash, HASH_KEY_EXPIRATION, 1)
end
end
end
yield
yield if unique
end
end
end
end

View file

@ -28,17 +28,19 @@ class TestClient < MiniTest::Unit::TestCase
describe 'with mock redis' do
before do
@redis = MiniTest::Mock.new
def @redis.multi; yield; end
def @redis.multi; yield if block_given?; end
def @redis.set(*); true; end
def @redis.sadd(*); true; end
def @redis.srem(*); true; end
def @redis.get(*); nil; end
def @redis.del(*); nil; end
def @redis.incrby(*); nil; end
def @redis.setex(*); nil; end
def @redis.setex(*); true; end
def @redis.expire(*); true; end
def @redis.watch(*); true; end
def @redis.with_connection; yield self; end
def @redis.with; yield self; end
def @redis.exec; true; end
Sidekiq.instance_variable_set(:@redis, @redis)
end