Rail 5使用自定义重载器交换ActionDispatch :: Reloader [英] Rail 5 swap ActionDispatch::Reloader with a custom reloader

查看:130
本文介绍了Rail 5使用自定义重载器交换ActionDispatch :: Reloader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用例,用于在本地开发时安装模拟引擎来处理会话,其中自定义会话中间件在请求到达时通过Net :: http请求调用模拟引擎.

We have a use case for mounting a mock engine to process sessions when developing locally in which a custom session middleware calls the mock engine via Net::http request when a request comes through.

代码更改时,将触发重新加载器,并

When there is code change, the reloader is triggered and here calls the ActiveSupport::Dependencies to start unloading. Then the request is pass down to our custom session middleware and the http request is fired. However since the http request calls to a mountable engine, it goes thought the same middlewares again and the reloader unloads all the dependencies again which cause the first reload to timeout. So the goal is to be able to skip the reload for the second request.

我将以下代码添加到ActionDispatch::Reloader

I added the follow code to ActionDispatch::Reloader here and it does exactly what I wanted.

class Reloader < Executor
  def initialize(app, executor)
    super(app, executor)
  end

  def call(env)
    request = ActionDispatch::Request.new(env)
    return @app.call(env) if skip_request?(request)
    super(env)
  end

  def skip_request?(request)        
    request.path_info.start_with?('/session')
  end
end

然后我想让这个清洁器算出来,将其完全拉出到模块中,然后从初始化程序进行这样的交换

Then I want to make this cleaner figured to pull that out completely to a module and just do a swap like this from an initializer

app.config.middleware.swap(::ActionDispatch::Reloader, MyModule::CustomReloaderMiddleware)

这里是模块

require 'action_dispatch'

module MyModule
  class CustomReloaderMiddleware < ActionDispatch::Executor
    def initialize(app, executor)
      @app, @executor = app, executor
    end

    def call(env)
      request = ActionDispatch::Request.new(env)
      return @app.call(env) if skip_request?(request)
      super(env)
    end

    def skip_request?(request)
      request.path_info.start_with?('/session')
    end
  end
end

但是我遇到了两个问题.

But I ran into a couple issues.

Uncaught exception: wrong number of arguments (given 1, expected 2).然后我尝试了以下

#1

def initialize(app, executor = nil)
  @app, @executor = app, executor
end

#2

def initialize(app, executor = nil)
  @app, @executor = app, ActiveSupport::Reloader
end

他们两个都正确启动了服务,并且我看到请求正在通过此中间件进行,但它没有重新加载代码..因此,我想知道用自定义重新加载器交换ActionDispatch :: Reloader的正确方法是什么?

Both of them starts the service correctly and I see the request going through this middleware but it does not reload the code.. So I wondered what is the correct way of swapping ActionDispatch::Reloader with a custom reloader ?

推荐答案

您需要将中间件的其他参数传递给swap调用:

You need to pass your middleware's additional argument to the swap call:

app.config.middleware.swap(::ActionDispatch::Reloader, MyModule::CustomReloaderMiddleware, app.reloader)

ActionDispatch::Reloader

That's the same argument given when ActionDispatch::Reloader is first added -- it's the application's reloader, which is a more specifically configured subclass of AS::Reloader (so you were on the right track).

这篇关于Rail 5使用自定义重载器交换ActionDispatch :: Reloader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆