diff --git a/lib/resque-lonely_job.rb b/lib/resque-lonely_job.rb index ce29cad..11ee9bd 100644 --- a/lib/resque-lonely_job.rb +++ b/lib/resque-lonely_job.rb @@ -6,7 +6,7 @@ module LonelyJob LOCK_TIMEOUT = 60 * 60 * 24 * 5 # 5 days def lock_timeout - Time.now.utc + LOCK_TIMEOUT + 1 + Time.now.to_i + LOCK_TIMEOUT + 1 end # Overwrite this method to uniquely identify which mutex should be used @@ -16,7 +16,15 @@ def redis_key(*args) end def can_lock_queue?(*args) - Resque.redis.setnx(redis_key(*args), lock_timeout) + 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 end def unlock_queue(*args) diff --git a/resque-lonely_job.gemspec b/resque-lonely_job.gemspec index 54a7d61..9d78dae 100644 --- a/resque-lonely_job.gemspec +++ b/resque-lonely_job.gemspec @@ -14,11 +14,12 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.version = Resque::Plugins::LonelyJob::VERSION - gem.add_dependency 'resque', '~> 1.20.0' + gem.add_dependency 'resque', '>= 1.20.0' gem.add_development_dependency 'mock_redis', '~> 0.4.1' gem.add_development_dependency 'rake' gem.add_development_dependency 'rspec' gem.add_development_dependency 'debugger' + gem.add_development_dependency 'timecop' gem.description = <