Skip to content

Commit

Permalink
Merge pull request #93 from artofhuman/fix-unlock
Browse files Browse the repository at this point in the history
fix: job not unlock if unique definded with block and hash
  • Loading branch information
artofhuman authored Feb 2, 2017
2 parents a87da05 + f01356c commit 46239b5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
3 changes: 3 additions & 0 deletions lib/resque/integration/unique.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'digest/sha1'

require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/hash'

require 'resque/plugins/lock'
require 'resque/plugins/progress'
Expand Down Expand Up @@ -66,6 +67,8 @@ def lock_on(&block)
# LockID should be independent from MetaID
# @api private
def lock(meta_id, *args)
args = [*args[0..-2], args.last.with_indifferent_access] if args.last.is_a?(Hash)

"lock:#{name}-#{Digest::SHA1.hexdigest(obj_to_string(lock_on[*args]))}"
end

Expand Down
1 change: 1 addition & 0 deletions resque-integration.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'railties', '>= 3.0.0'
gem.add_runtime_dependency 'activerecord', '>= 3.0.0'
gem.add_runtime_dependency 'actionpack', '>= 3.0.0'
gem.add_runtime_dependency 'activesupport', '>= 3.0.0'
gem.add_runtime_dependency 'resque-lock', '~> 1.1.0'
gem.add_runtime_dependency 'resque-meta', '>= 2.0.0'
gem.add_runtime_dependency 'resque-progress', '~> 1.0.1'
Expand Down
62 changes: 56 additions & 6 deletions spec/resque/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,92 @@

describe 'enqueue' do
context 'when job is uniq' do
class DummyService
def self.call
# no-op
end
end

class UniqueJob
include Resque::Integration

queue :test
unique

def self.execute(id, params)
DummyService.call
end
end

it 'enqueues only one job' do
UniqueJob.enqueue(param: 'one')
UniqueJob.enqueue(1, param: 'one')

Timecop.travel(10.hours.since) do
UniqueJob.enqueue(param: 'one')
UniqueJob.enqueue(1, param: 'one')

expect(Resque.peek(:test, 0, 100).size).to eq(1)
end
end

it 'enqueues two jobs with differ args' do
UniqueJob.enqueue(param: 'one')
UniqueJob.enqueue(1, param: 'one')

Timecop.travel(10.hours.since) do
UniqueJob.enqueue(param: 'two')
UniqueJob.enqueue(1, param: 'two')

expect(Resque.peek(:test, 0, 100).size).to eq(2)
end
end

it 'enqueues two jobs after expire lock timeout' do
UniqueJob.enqueue(param: 'one')
UniqueJob.enqueue(1, param: 'one')

Timecop.travel(4.days.since) do
UniqueJob.enqueue(param: 'one')
UniqueJob.enqueue(1, param: 'one')

expect(Resque.peek(:test, 0, 100).size).to eq(2)
end
end

describe 'unlock' do
class UniqueJobWithBlock
include Resque::Integration

queue :test_with_block
unique { |id, params| [id, params[:one], params[:two]] }

def self.execute(id, params)
DummyService.call
end
end

around do |example|
inline = Resque.inline
Resque.inline = true

example.run

Resque.inline = inline
end

it 'unlocks uniq job with args and without block' do
expect(DummyService).to receive(:call).twice

UniqueJob.enqueue(1, one: 1, two: 2)
UniqueJob.enqueue(1, one: 1, two: 2)

expect(UniqueJob.locked?(1, one: 1, two: 2)).to eq(false)
end

it 'unlocks uniq job with args and block' do
expect(DummyService).to receive(:call).twice

UniqueJobWithBlock.enqueue(1, one: 1, two: 2)
UniqueJobWithBlock.enqueue(1, one: 1, two: 2)

expect(UniqueJobWithBlock.locked?(1, one: 1, two: 2)).to eq(false)
end
end
end
end
end

0 comments on commit 46239b5

Please sign in to comment.