1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Resolve Rails Reloader and ActiveRecord middleware incompatibility (#3166)

* Prevent AR middleware and reloader clash

The ActiveRecord middleware releases any current ActiveRecord
connections before the reloader has a chance to clear their query
caches.

We can just remove this middleware because the reloader takes care of
clearing active connections, too.

* Make sure folks haven't added the middleware manually

* Fix the conditional

* Mention that the reloader also clears connections

* Move it all back into a single method

* Typos

* Pull all rails setup into railtie

* Set the middleware even earlier

Also clarify exactly when these hooks are run.

* Only check ActiveRecord reloader/middleware sanity at boot
This commit is contained in:
Samuel Cochran 2016-10-01 08:27:54 +10:00 committed by Mike Perham
parent 6b14010199
commit 659dea9601
3 changed files with 33 additions and 5 deletions

View file

@ -150,10 +150,6 @@ module Sidekiq
Middleware::Chain.new do |m|
m.add Middleware::Server::Logging
m.add Middleware::Server::RetryJobs
if defined?(::ActiveRecord::Base)
require 'sidekiq/middleware/server/active_record'
m.add Sidekiq::Middleware::Server::ActiveRecord
end
end
end

View file

@ -240,7 +240,6 @@ module Sidekiq
else
require 'sidekiq/rails'
require File.expand_path("#{options[:require]}/config/environment.rb")
Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
end
options[:tag] ||= default_tag
else

View file

@ -32,10 +32,43 @@ module Sidekiq
end
class Rails < ::Rails::Engine
# We need to setup this up before any application configuration which might
# change Sidekiq middleware.
#
# This hook happens after `Rails::Application` is inherited within
# config/application.rb and before config is touched, usually within the
# class block. Definitely before config/environments/*.rb and
# config/initializers/*.rb.
config.before_configuration do
if ::Rails::VERSION::MAJOR < 5 && defined?(::ActiveRecord)
Sidekiq.server_middleware do |chain|
require 'sidekiq/middleware/server/active_record'
chain.add Sidekiq::Middleware::Server::ActiveRecord
end
end
end
initializer 'sidekiq' do
Sidekiq.hook_rails!
end
# We have to add the reloader after initialize to see if cache_classes has
# been turned on.
#
# This hook happens after all initialziers are run, just before returning
# from config/environment.rb back to sidekiq/cli.rb.
config.after_initialize do
if ::Rails::VERSION::MAJOR >= 5
# The reloader also takes care of ActiveRecord but is incompatible with
# the ActiveRecord middleware so make sure it's not in the chain already.
if defined?(Sidekiq::Middleware::Server::ActiveRecord) && Sidekiq.server_middleware.exists?(Sidekiq::Middleware::Server::ActiveRecord)
raise ArgumentError, "You are using the Sidekiq ActiveRecord middleware and the new Rails 5 reloader which are incompatible. Please remove the ActiveRecord middleware from your Sidekiq middleware configuration."
else
Sidekiq.options[:reloader] = Sidekiq::Rails::Reloader.new
end
end
end
class Reloader
def initialize(app = ::Rails.application)
Sidekiq.logger.debug "Enabling Rails 5+ live code reloading, so hot!" unless app.config.cache_classes