Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unlock queue when calling dequeue to prevent deadlocks.

  • Loading branch information...
commit 740f1ab526acb06d2dd786725309b9103fe08d06 1 parent 3a022fe
@humancopy authored
Showing with 16 additions and 6 deletions.
  1. +7 −3 lib/resque/plugins/lock.rb
  2. +9 −3 test/lock_test.rb
View
10 lib/resque/plugins/lock.rb
@@ -45,11 +45,15 @@ module Lock
# passed the same arguments as `perform`, that is, your job's
# payload.
def lock(*args)
- "lock:#{name}-#{args.to_s}"
+ "#{name}-#{args.to_s}"
end
def before_enqueue_lock(*args)
- Resque.redis.setnx(lock(*args), true)
+ Resque.redis.hsetnx('resque-lock', lock(*args), true)
+ end
+
+ def before_dequeue_lock(*args)
+ Resque.redis.hdel('resque-lock', lock(*args))
end
def around_perform_lock(*args)
@@ -58,7 +62,7 @@ def around_perform_lock(*args)
ensure
# Always clear the lock when we're done, even if there is an
# error.
- Resque.redis.del(lock(*args))
+ Resque.redis.hdel('resque-lock', lock(*args))
end
end
end
View
12 test/lock_test.rb
@@ -2,8 +2,6 @@
require 'resque'
require 'resque/plugins/lock'
-$counter = 0
-
class LockTest < Test::Unit::TestCase
class Job
extend Resque::Plugins::Lock
@@ -16,7 +14,7 @@ def self.perform
def setup
Resque.redis.del('queue:lock_test')
- Resque.redis.del(Job.lock)
+ Resque.redis.hdel('resque-lock', Job.lock)
end
def test_lint
@@ -30,11 +28,19 @@ def test_version
assert_equal 1, major.to_i
assert minor.to_i >= 17
assert Resque::Plugin.respond_to?(:before_enqueue_hooks)
+ assert Resque::Plugin.respond_to?(:before_dequeue_hooks)
end
def test_lock
3.times { Resque.enqueue(Job) }
assert_equal 1, Resque.redis.llen('queue:lock_test')
+ assert_equal "true", Resque.redis.hget('resque-lock', Job.lock)
+ end
+
+ def test_unlock
+ Resque.enqueue(Job)
+ Resque.dequeue(Job)
+ assert_equal nil, Resque.redis.hget('resque-lock', Job.lock)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.