2015-12-31 18:33:35 -05:00
# frozen_string_literal: true
2012-01-25 16:32:51 -05:00
module Sidekiq
2012-03-10 15:30:15 -05:00
def self . hook_rails!
2014-06-28 18:19:42 -04:00
return if defined? ( @delay_removed )
2014-03-28 20:36:38 -04:00
ActiveSupport . on_load ( :active_record ) do
include Sidekiq :: Extensions :: ActiveRecord
2012-03-10 15:30:15 -05:00
end
2014-03-28 20:36:38 -04:00
ActiveSupport . on_load ( :action_mailer ) do
extend Sidekiq :: Extensions :: ActionMailer
2012-03-10 15:30:15 -05:00
end
2014-06-28 18:19:42 -04:00
Module . __send__ ( :include , Sidekiq :: Extensions :: Klass )
2012-03-10 15:30:15 -05:00
end
2014-04-24 10:11:05 -04:00
# Removes the generic aliases which MAY clash with names of already
# created methods by other applications. The methods `sidekiq_delay`,
# `sidekiq_delay_for` and `sidekiq_delay_until` can be used instead.
2014-05-20 23:42:25 -04:00
def self . remove_delay!
2014-06-28 18:19:42 -04:00
@delay_removed = true
2014-05-20 23:42:25 -04:00
[ Extensions :: ActiveRecord ,
Extensions :: ActionMailer ,
2014-04-24 10:11:05 -04:00
Extensions :: Klass ] . each do | mod |
mod . module_eval do
remove_method :delay if respond_to? ( :delay )
remove_method :delay_for if respond_to? ( :delay_for )
remove_method :delay_until if respond_to? ( :delay_until )
end
end
end
2012-01-25 16:32:51 -05:00
class Rails < :: Rails :: Engine
2016-09-30 18:27:54 -04:00
# 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
2012-03-10 15:30:15 -05:00
initializer 'sidekiq' do
Sidekiq . hook_rails!
end
2016-02-01 18:59:20 -05:00
2016-09-30 18:27:54 -04:00
# 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. "
2016-10-17 13:54:46 -04:00
elsif :: Rails . application . config . cache_classes
# The reloader API has proven to be troublesome under load in production.
# We won't use it at all when classes are cached, see #3154
Sidekiq . logger . debug { " Autoload disabled in #{ :: Rails . env } , Sidekiq will not reload changed classes " }
2016-11-12 09:17:36 -05:00
Sidekiq . options [ :executor ] = Sidekiq :: Rails :: Executor . new
2016-09-30 18:27:54 -04:00
else
2016-11-12 09:17:36 -05:00
Sidekiq . logger . debug { " Enabling Rails 5+ live code reloading, so hot! " }
2016-09-30 18:27:54 -04:00
Sidekiq . options [ :reloader ] = Sidekiq :: Rails :: Reloader . new
2016-11-22 12:21:16 -05:00
Psych :: Visitors :: ToRuby . prepend ( Sidekiq :: Rails :: PsychAutoload )
2016-09-30 18:27:54 -04:00
end
end
end
2016-11-12 09:17:36 -05:00
class Executor
def initialize ( app = :: Rails . application )
@app = app
end
def call
@app . executor . wrap do
yield
end
end
def inspect
" # <Sidekiq::Rails::Executor @app= #{ @app . class . name } > "
end
end
2016-02-01 18:59:20 -05:00
class Reloader
def initialize ( app = :: Rails . application )
@app = app
end
def call
2016-04-28 13:02:03 -04:00
@app . reloader . wrap do
yield
2016-02-01 18:59:20 -05:00
end
end
2016-04-28 13:38:46 -04:00
def inspect
" # <Sidekiq::Rails::Reloader @app= #{ @app . class . name } > "
end
2016-02-01 18:59:20 -05:00
end
2016-11-22 12:21:16 -05:00
module PsychAutoload
def resolve_class ( klass_name )
klass_name && klass_name . constantize
rescue NameError
super
end
end
2012-03-10 15:30:15 -05:00
end if defined? ( :: Rails )
2012-01-25 16:32:51 -05:00
end