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

add sidekiq middleware component to avoid reusing contexts between wo… #232

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
9 changes: 8 additions & 1 deletion lib/makara.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'active_support'
require 'makara/version'
require 'makara/railtie' if defined?(Rails)
if defined?(Rails)
require 'makara/railtie'
require 'makara/sidekiq/railtie' if defined?(Sidekiq)
end
module Makara

autoload :Cache, 'makara/cache'
Expand Down Expand Up @@ -31,6 +34,10 @@ module Strategies
autoload :PriorityFailover, 'makara/strategies/priority_failover'
end

module Sidekiq
autoload :Middleware, 'makara/sidekiq/middleware'
end

end

ActiveSupport.on_load(:active_record) do
Expand Down
11 changes: 11 additions & 0 deletions lib/makara/sidekiq/middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Makara
module Sidekiq
class Middleware
def call(*args)
yield
ensure
::Makara::Context.set_current({})
end
end
end
end
13 changes: 13 additions & 0 deletions lib/makara/sidekiq/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Makara
module Sidekiq
class Railtie < ::Rails::Railtie
initializer 'makara-sidekiq.insert_middleware' do |app|
::Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.add Makara::Sidekiq::Middleware
end
end
end
end
end
end
32 changes: 32 additions & 0 deletions spec/sidekiq/middleware_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'makara/sidekiq/middleware'

describe Makara::Sidekiq::Middleware do
let(:worker) { class_double('Worker') }
let(:job) { Hash.new }
let(:queue) { 'default' }

shared_examples 'a cleared context' do
it 'clears the context' do
expect(::Makara::Context).to receive(:set_current).with({})

subject.call(worker, job, queue) {}
end
end

context 'when the worker raises an error' do
before do
allow_any_instance_of(worker).to receive(:perform).and_raise(ArgumentError)
end

it_behaves_like 'a cleared context'
end

context 'when the worker completes successfully' do
before do
allow_any_instance_of(worker).to receive(:perform).and_return(true)
end

it_behaves_like 'a cleared context'
end
end