Skip to content

Commit

Permalink
Fix converting legacy locks (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Feb 28, 2019
1 parent 2ede46e commit 99eed96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
16 changes: 10 additions & 6 deletions lib/sidekiq_unique_jobs/locksmith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,24 @@ def unlock!(token = nil)
# @return [true, false]
def locked?(token = nil)
token ||= jid
Scripts.call(
:convert_legacy_lock,
redis_pool,
keys: [grabbed_key, unique_digest],
argv: [token],
)

convert_legacy_lock(token)
redis(redis_pool) { |conn| conn.hexists(grabbed_key, token) }
end

private

attr_reader :unique_digest, :ttl, :jid, :redis_pool, :lock_type

def convert_legacy_lock(token)
Scripts.call(
:convert_legacy_lock,
redis_pool,
keys: [grabbed_key, unique_digest],
argv: [token, current_time.to_f],
)
end

def grab_token(timeout = nil)
redis(redis_pool) do |conn|
if timeout.nil? || timeout.positive?
Expand Down
12 changes: 2 additions & 10 deletions redis/convert_legacy_lock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ local grabbed_key = KEYS[1]
local unique_digest = KEYS[2]

local job_id = ARGV[1]

local function current_time()
local time = redis.call('time')
local s = time[1]
local ms = time[2]
local number = tonumber((s .. '.' .. ms))

return number
end
local current_time = tonumber(ARGV[2])

local old_token = redis.call('GET', unique_digest)
if old_token then
if old_token == job_id or old_token == '2' then
redis.call('DEL', unique_digest)
redis.call('HSET', grabbed_key, job_id, current_time())
redis.call('HSET', grabbed_key, job_id, current_time)
end
end

0 comments on commit 99eed96

Please sign in to comment.