Skip to content

Commit

Permalink
fix: Expire unique lock unless it deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Dec 10, 2017
1 parent cdc53a0 commit 9339287
Showing 1 changed file with 6 additions and 19 deletions.
25 changes: 6 additions & 19 deletions lib/resque/integration/unique.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,18 @@ def before_dequeue_lock(*args)
!meta.working?
end

# Before enqueue acquire a lock
#
# Returns boolean
def before_enqueue_lock(*args)
key = lock(*args)
now = ::Time.current.to_i
timeout = now + lock_timeout + 1

# return true if we successfully acquired the lock
return true if ::Resque.redis.setnx(key, timeout)

# see if the existing timeout is still valid and return false if it is
# (we cannot acquire the lock during the timeout period)
return false if now <= ::Resque.redis.get(key).to_i

# otherwise set the timeout and ensure that no other worker has
# acquired the lock
now > ::Resque.redis.getset(key, timeout).to_i
::Resque.redis.set(lock(*args), 1, ex: lock_timeout, nx: true)
end

def around_perform_lock(*args)
yield
ensure
# Always clear the lock when we're done, even if there is an error.
::Resque.redis.del(lock(*args))
unlock(*args)
end

# When job is dequeued we should remove lock
Expand All @@ -163,10 +153,7 @@ def lock_timeout

# Returns true if resque job is in locked state
def locked?(*args)
key = lock(nil, *args)
now = Time.now.to_i

::Resque.redis.exists(key) && now <= ::Resque.redis.get(key).to_i
::Resque.redis.exists(lock(nil, *args))
end

# Dequeue unique job
Expand Down

0 comments on commit 9339287

Please sign in to comment.