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

Update lonely_job to use new locking pattern #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions lib/resque-lonely_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,17 @@ module Plugins
module LonelyJob
LOCK_TIMEOUT = 60 * 60 * 24 * 5 # 5 days

def lock_timeout
Time.now.to_i + LOCK_TIMEOUT + 1
end

# Overwrite this method to uniquely identify which mutex should be used
# for a resque worker.
def redis_key(*args)
"lonely_job:#{@queue}"
end

def can_lock_queue?(*args)
now = Time.now.to_i
key = redis_key(*args)
timeout = lock_timeout

# Per http://redis.io/commands/setnx
return true if Resque.redis.setnx(key, timeout)
return false if Resque.redis.get(key).to_i > now
return true if Resque.redis.getset(key, timeout).to_i <= now
return false
# Per http://redis.io/commands/set
Resque.redis.set key, 'anystring', nx: true, ex: LOCK_TIMEOUT
end

def unlock_queue(*args)
Expand Down
4 changes: 2 additions & 2 deletions resque-lonely_job.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Resque::Plugins::LonelyJob::VERSION

gem.add_dependency 'resque', '>= 1.20.0'
gem.add_development_dependency 'mock_redis', '~> 0.4.1'
gem.add_dependency 'resque', '~> 1.24.0'
gem.add_development_dependency 'mock_redis'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'debugger'
Expand Down