-
Notifications
You must be signed in to change notification settings - Fork 72
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
base: master
Are you sure you want to change the base?
Changes from all commits
3a022fe
740f1ab
74f23f1
2badb41
7149f31
1218d52
240cdc7
467a2e2
a59aabd
ad6f9fa
9a62f9f
52b7407
4a08c3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
source :rubygems | ||
|
||
gem "resque" | ||
gem "resque", :git => 'git://github.com/humancopy/resque.git' | ||
|
||
group :development do | ||
gem "turn" | ||
gem "rake" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,27 +40,62 @@ module Plugins | |
# UpdateNetworkGraph is running at a time, regardless of the | ||
# repo_id. Normally a job is locked using a combination of its | ||
# class name and arguments. | ||
# | ||
# It is also possible to define locks which will get released | ||
# BEFORE performing a job by overriding the lock_running? class | ||
# method in your subclass. This is useful in cases where you need | ||
# to get a job queued even if another job on same queue is already | ||
# running, e.g. | ||
# | ||
# class UpdateNetworkGraph | ||
# extend Resque::Plugins::Lock | ||
# | ||
# # Do not lock a running job | ||
# def self.lock_running? | ||
# false | ||
# end | ||
# end | ||
module Lock | ||
# Override in your job to control the lock key. It is | ||
# 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 namespaced_lock(*args) | ||
"lock:#{lock(*args)}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
end | ||
|
||
def before_enqueue_lock(*args) | ||
Resque.redis.setnx(lock(*args), true) | ||
Resque.redis.setnx(namespaced_lock(*args), true) | ||
end | ||
|
||
def before_dequeue_lock(*args) | ||
Resque.redis.del(namespaced_lock(*args)) | ||
end | ||
|
||
def lock_running? | ||
true | ||
end | ||
|
||
def around_perform_lock(*args) | ||
before_dequeue_lock(*args) unless lock_running? | ||
begin | ||
yield | ||
ensure | ||
# Always clear the lock when we're done, even if there is an | ||
# error. | ||
Resque.redis.del(lock(*args)) | ||
before_dequeue_lock(*args) if lock_running? | ||
end | ||
end | ||
|
||
def self.all_locks | ||
Resque.redis.keys('lock:*') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the redis documentation for keys:
This is also unrelated to the purpose of the pull request. A pull request should focus on fixing one thing to make it easier to get merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resque-lock should really be using a set instead of separate keys to make it easy to get a list of locks and check if locks aren't being cleaned up. I have a commit to use redis sets, but I will wait for #2 and #6 to be merged before opening a pull request to avoid conflicts. |
||
end | ||
def self.clear_all_locks | ||
all_locks.collect { |x| Resque.redis.del(x) }.count | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough!