在完成重新加载之前,Rails 5重新加载器转移到下一个中​​间件

在rails 5开发模式中,在代码更改和刷新之后,它会触发重新加载器中间件来卸载/重新加载类。 但在重新加载重新加载之前,请求将移至下一个中间件。

重新加载器包含执行程序并查看executor.rb 。 似乎下一个中间件在run!后被调用run!complete!之前complete! 这与以下日志消息一致。 我想知道我是否可以确保下一个中间件仅在重新加载后调用。

我试图用Rails.application.executor.wrapRails.application.reloader.wrapActiveSupport::Dependencies.interlock.permit_concurrent_loads包装cusstomMiddle调用,但结果不是那些工作。

Rails版本:5.1

Ruby版本:2.4.1

我添加了以下日志消息,请参阅事件序列。

application.rb中

 ActiveSupport::Reloader.before_class_unload do puts "Before class unload, Thread Id: #{Thread.current.object_id}" end ActiveSupport::Reloader.after_class_unload do puts "After class unload, Thread Id: #{Thread.current.object_id}" end ActiveSupport::Reloader.to_run do puts "Reloading, Thread Id: #{Thread.current.object_id}" end ActiveSupport::Reloader.to_complete do puts "DONE Reloading, Thread Id: #{Thread.current.object_id}" end 

custom_middleware.rb

 def call(env) puts "Call custom middleware, Thread Id: #{Thread.current.object_id}" @app.call(env) end 

以下是我得到的输出,并注意到“调用自定义中间件”在“完成重新加载”之前被调用

 Started GET "/" for 127.0.0.1 at 2018-07-06 01:59:54 -0500 Before class unload, Thread Id: 70134855012820 After class unload, Thread Id: 70134855012820 Reloading, Thread Id: 70134855012820 Call custom middleware, Thread Id: 70134855012820 Processing by HomeController#index as HTML Rendering home/index.html.erb Rendered home/index.html.erb (0.3ms) Completed 200 OK in 17ms (Views: 13.5ms) DONE Reloading, Thread Id: 70134855012820 

我所有的中间件

 use Rack::Sendfile use ActionDispatch::Static use ActionDispatch::Executor use ActiveSupport::Cache::Strategy::LocalCache::Middleware use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use RequestStore::Middleware use ActionDispatch::RemoteIp use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::Migration::CheckPending use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use Rack::Head use Rack::ConditionalGet use Rack::ETag use MyCustomSession::CustomMiddleware