Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlock queue on dequeue #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ source :rubygems
gem "resque"

group :development do
gem "turn"
gem "rake"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't run tests with this gem. Was getting this error message:

~/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/test/unit/assertions.rb:5:in module:Test': Unit is not a module (TypeError)`

The tests run without it and the rake file is testing if the command exists, so i figured it should be optional. :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough!

end
10 changes: 7 additions & 3 deletions lib/resque/plugins/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions test/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'resque'
require 'resque/plugins/lock'

$counter = 0

class LockTest < Test::Unit::TestCase
class Job
extend Resque::Plugins::Lock
Expand All @@ -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
Expand All @@ -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_nil Resque.redis.hget('resque-lock', Job.lock)
end
end