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

Normalize args when deriving the redis lock key. #6

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion lib/resque/plugins/lock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def lock(*args)
end

def before_enqueue_lock(*args)
Resque.redis.setnx(lock(*args), true)
normalized_args = Resque.decode(Resque.encode(args))
Resque.redis.setnx(lock(*normalized_args), true)
end

def around_perform_lock(*args)
Expand Down
33 changes: 28 additions & 5 deletions test/lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@
require 'resque'
require 'resque/plugins/lock'

$counter = 0

class LockTest < Test::Unit::TestCase
class Job
extend Resque::Plugins::Lock
@queue = :lock_test

def self.perform
raise "Woah woah woah, that wasn't supposed to happen"
class << self
attr_accessor :counter
end

def self.perform(params={})
@counter += 1
end
end

def setup
Job.counter = 0
Resque.redis.keys('lock:*').each { |key| Resque.redis.del(key) }
Resque.redis.del('queue:lock_test')
Resque.redis.del(Job.lock)
end

def test_lint
Expand All @@ -36,5 +39,25 @@ def test_lock
3.times { Resque.enqueue(Job) }

assert_equal 1, Resque.redis.llen('queue:lock_test')
assert Resque.redis.exists(Job.lock)
end

def test_perform_with_args
args = [{ 'a' => 1, :b => 2.0, 'c' => '3' }]
lock_key = Job.lock(*Resque.decode(Resque.encode(args)))
Resque.enqueue(Job, *args)
assert Resque.redis.exists(lock_key)
work_off_jobs
assert !Resque.redis.exists(lock_key)
assert_equal 1, Job.counter
end

private

def work_off_jobs
worker = Resque::Worker.new('*')
while job = worker.reserve
worker.perform(job)
end
end
end