Skip to content

Commit

Permalink
Close #97
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Aug 30, 2015
1 parent 0c3205b commit 46f8070
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 43 deletions.
32 changes: 31 additions & 1 deletion lib/sidekiq_unique_jobs/sidekiq_unique_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@
require 'sidekiq_unique_jobs/server/extensions'

module Sidekiq
class SortedEntry
module UniqueExtension
include SidekiqUniqueJobs::Server::Extensions

def self.included(base)
base.class_eval do
alias_method :delete_orig, :delete
alias_method :delete, :delete_ext
alias_method :remove_job_orig, :remove_job
alias_method :remove_job, :remove_job_ext
end
end

def delete_ext
unlock(payload_hash(item), item)
delete_orig
end

def remove_job_ext
remove_job_orig do |message|
unlock(payload_hash(Sidekiq.load_json(message)), item)
yield message
end
end
end
include UniqueExtension
end

class Job
module UniqueExtension
SCHEDULED ||= 'schedule'.freeze
include SidekiqUniqueJobs::Server::Extensions
extend Forwardable
def_delegator :SidekiqUniqueJobs, :payload_hash

def self.included(base)
base.class_eval do
alias_method :delete_orig, :delete
Expand All @@ -24,7 +54,7 @@ def delete_ext
def unlock(lock_key, item)
Sidekiq.redis do |con|
con.eval(remove_on_match, keys: [lock_key], argv: [item['jid']])
if @parent && @parent.name == 'schedule'.freeze
if defined?(@parent) && @parent && @parent.name == SCHEDULED
con.eval(remove_scheduled_on_match, keys: [lock_key], argv: [item['jid']])
else
con.eval(remove_on_match, keys: [lock_key], argv: [item['jid']])
Expand Down
91 changes: 49 additions & 42 deletions spec/lib/sidekiq_unique_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,73 @@
require 'sidekiq_unique_jobs/client/middleware'
require 'sidekiq_unique_jobs/sidekiq_unique_ext'

class JustAWorker
include Sidekiq::Worker

sidekiq_options unique: true, queue: 'testqueue'
RSpec.describe 'Sidekiq::Api' do
class JustAWorker
include Sidekiq::Worker

def perform
sidekiq_options unique: true, queue: 'testqueue'

def perform
end
end
end

RSpec.describe Sidekiq::Job::UniqueExtension do
before do
Sidekiq.redis = REDIS
Sidekiq.redis(&:flushdb)
end

it 'deletes uniqueness lock on delete' do
params = { foo: 'bar' }
payload_hash = SidekiqUniqueJobs.get_payload('JustAWorker', 'testqueue', [params])
jid = JustAWorker.perform_async(foo: 'bar')
queue = Sidekiq::Queue.new('testqueue')
job = queue.find_job(jid)
job.delete
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end
end
end
let(:params) { { foo: 'bar' } }
let(:payload_hash) { SidekiqUniqueJobs.get_payload('JustAWorker', 'testqueue', [params]) }

RSpec.describe Sidekiq::Queue::UniqueExtension do
before do
Sidekiq.redis = REDIS
Sidekiq.redis(&:flushdb)
def schedule_job
JustAWorker.perform_in(60 * 60 * 3, params)
end

it 'deletes uniqueness locks on clear' do
params = { foo: 'bar' }
payload_hash = SidekiqUniqueJobs.get_payload('JustAWorker', 'testqueue', [params])
def perform_async
JustAWorker.perform_async(foo: 'bar')
queue = Sidekiq::Queue.new('testqueue')
queue.clear
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end

describe Sidekiq::SortedEntry::UniqueExtension do
it 'deletes uniqueness lock on delete' do
schedule_job

Sidekiq::ScheduledSet.new.each(&:delete)
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end

expect(schedule_job).not_to eq(nil)
end
end
end

RSpec.describe Sidekiq::JobSet::UniqueExtension, sidekiq_ver: 3 do
before do
Sidekiq.redis = REDIS
Sidekiq.redis(&:flushdb)
describe Sidekiq::Job::UniqueExtension do
it 'deletes uniqueness lock on delete' do
jid = perform_async
Sidekiq::Queue.new('testqueue').find_job(jid).delete
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end
end
end

describe Sidekiq::Queue::UniqueExtension do
it 'deletes uniqueness locks on clear' do
perform_async
Sidekiq::Queue.new('testqueue').clear
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end
end
end

it 'deletes uniqueness locks on clear' do
params = { foo: 'bar' }
payload_hash = SidekiqUniqueJobs.get_payload('JustAWorker', 'testqueue', [params])
JustAWorker.perform_in(60 * 60 * 3, foo: 'bar')
set = Sidekiq::JobSet.new('schedule')
set.clear
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
describe Sidekiq::JobSet::UniqueExtension, sidekiq_ver: 3 do
it 'deletes uniqueness locks on clear' do
schedule_job
Sidekiq::JobSet.new('schedule').clear
Sidekiq.redis do |c|
expect(c.exists(payload_hash)).to be_falsy
end
end
end
end

0 comments on commit 46f8070

Please sign in to comment.