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

Do not unlock on sidekiq shutdown #87

Merged
merged 6 commits into from
Jun 22, 2015
Merged
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
6 changes: 5 additions & 1 deletion lib/sidekiq_unique_jobs/middleware/server/unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ class UniqueJobs
attr_reader :unlock_order, :redis_pool

def call(worker, item, _queue, redis_pool = nil)
shutdown = false
@redis_pool = redis_pool

decide_unlock_order(worker.class)
lock_key = payload_hash(item)
unlocked = before_yield? ? unlock(lock_key).inspect : 0

yield
rescue Sidekiq::Shutdown
shutdown = true
raise
ensure
if after_yield? || !defined? unlocked || unlocked != 1
if !shutdown && (after_yield? || !defined? unlocked || unlocked != 1)
unlock(lock_key)
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/middleware/server/unique_jobs_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'sidekiq/cli'

module SidekiqUniqueJobs
module Middleware
Expand Down Expand Up @@ -75,6 +76,31 @@ module Server
expect(subject.default_unlock_order).to eq(:after_yield)
end
end

describe '#call' do
context 'unlock' do
let(:uj) { SidekiqUniqueJobs::Middleware::Server::UniqueJobs.new }
let(:items) { [AfterYieldWorker.new, { 'class' => 'testClass' }, 'test'] }

it 'should unlock after yield when call succeeds' do
expect(uj).to receive(:unlock)

uj.call(*items) { true }
end

it 'should unlock after yield when call errors' do
expect(uj).to receive(:unlock)

expect { uj.call(*items) { fail } }.to raise_error(RuntimeError)
end

it 'should not unlock after yield on shutdown, but still raise error' do
expect(uj).to_not receive(:unlock)

expect { uj.call(*items) { fail Sidekiq::Shutdown } }.to raise_error(Sidekiq::Shutdown)
end
end
end
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions spec/support/after_yield_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AfterYieldWorker
include Sidekiq::Worker
sidekiq_options queue: :working, retry: 1, backtrace: 10, unique_unlock_order: :after_yield
sidekiq_options unique: false

sidekiq_retries_exhausted do |msg|
Sidekiq.logger.warn "Failed #{msg['class']} with #{msg['args']}: #{msg['error_message']}"
end

def perform(*)
# NO-OP
end
end