diff --git a/Gemfile b/Gemfile index d7590a2..78c4049 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,5 @@ gem "resque" group :development do gem "turn" gem "rake" + gem 'json' end diff --git a/Rakefile b/Rakefile index 97fd597..5aba4c0 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,4 @@ require 'rake/testtask' -require 'rake/rdoctask' def command?(command) system("type #{command} > /dev/null") diff --git a/lib/resque/plugins/lock.rb b/lib/resque/plugins/lock.rb index dccfff2..8bccb61 100644 --- a/lib/resque/plugins/lock.rb +++ b/lib/resque/plugins/lock.rb @@ -70,8 +70,9 @@ def before_enqueue_lock(*args) # (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 + # otherwise dequeue the job holding the current lock and reset the + # timeout to ensure that no other worker has acquired the lock + Resque.dequeue(self, *args) now > Resque.redis.getset(key, timeout).to_i end @@ -84,6 +85,11 @@ def around_perform_lock(*args) Resque.redis.del(lock(*args)) end end + + # Some errors may not be capture by above `ensure'. Handle it here. + def on_failure_lock(exception, *args) + Resque.redis.del(lock(*args)) + end end end end diff --git a/resque-lock.gemspec b/resque-lock.gemspec index 7ad3d36..b915db5 100644 --- a/resque-lock.gemspec +++ b/resque-lock.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "resque-lock" - s.version = "1.1.0" + s.version = "1.1.3" s.date = Time.now.strftime('%Y-%m-%d') s.summary = "A Resque plugin for ensuring only one instance of your job is queued at a time." s.homepage = "http://github.com/defunkt/resque-lock" diff --git a/test/lock_test.rb b/test/lock_test.rb index 5e4a25c..3239cfe 100644 --- a/test/lock_test.rb +++ b/test/lock_test.rb @@ -55,6 +55,6 @@ def test_deadlock sleep 3 Resque.enqueue(Job) - assert_equal 2, Resque.redis.llen('queue:lock_test') + assert_equal 1, Resque.redis.llen('queue:lock_test') end end