繁体   English   中英

如何强制我的插件重新加载每个请求?

[英]How can I force my plugin to reload with each request?

据我了解,在开发模式下每个请求都不会在Rails中重新加载插件。 这是有道理的,因为通常您将插件添加到您的应用程序,它是您正在开发的应用程序。

但是如果你正在开发一个插件,你必须重新启动服务器,每次更改插件都会产生很大的开销。

有没有办法让Rails在开发过程中重新加载你的插件,重新加载模型和控制器的方式?

我也一直在努力解决这个问题。 这些解决方案都不起作用,包括autoload_pathsautoload_once_paths技巧。 此外,使用FileUpdateChecker和初始化程序的hack也无济于事(检查程序正确触发,但文件未重新加载)。 config.reload_plugins = true相同...

但是,有一个解决方案。 app/controllers/application_controller.rb添加一行: require_dependency 'your_file_name_here'应用控制器重新加载在每次请求和require_dependency使得用于修改要被检查,并相应地重新加载文件。 它适用于我,Apache 2,Passenger 3和Rails 3.0.3。

我是通过使用猎枪宝石来做到这一点的。

gem install shotgun

cd /path/to/rails/app

shotgun

响应时间较慢,但重新加载所有rails代码,而不是浪费时间编写autoload_paths

简单的方法,在“app”文件夹内的文件夹中开发插件:

  • 应用
    • 楷模
    • 控制器
    • 助手
    • 意见
    • your_plugin_here

这样,您的所有插件类都将在每个请求上重新加载。

另一种可能性是在application.rb文件中添加路径:

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)

module SunspotTutorial
  class Application < Rails::Application

    config.autoload_paths += %W{ #{config.root}/plugins/your_plugin_name/lib }

    #lots of other code
  end
end

这样你的插件就会一直重新加载。

如果在插件代码更改时自动重新启动服务器是一个可接受的解决方案,那么您可以使用Mike Clark / topfunky的rstakeout ,或者更新的watchr ,它听起来像是做同样的事情。

你会做这样的事情:

rstakeout 'touch tmp/restart.txt' 'vendor/plugins/**/*'

使用https://github.com/ranmocy/guard-rails ,它非常简单:

# Gemfile.local
gem 'guard-rails'


$ bundle
$ guard init rails


# Guardfile:
guard 'rails' do
  watch('Gemfile.lock')
  watch(%r{^(config|plugins)/.*})
end


$ bundle exec guard

这个解决方案适用于引擎,适用于Rails 2.3,但附带一个cavaet,它会在每个请求中加载引擎和父应用程序中的所有文件,这将使响应时间变慢。

# lib/my_engine/engine.rb
if ENV['RELOAD_MYENGINE'] && Rails.env.development?
  config.to_prepare do
    Rails.logger.debug "RELOADING MY ENGINE"
    require_dependency MyEngine::Engine.root.join('lib', 'my_engine').to_s
  end

config.after_initialize do
  Rails.application.config.reload_classes_only_on_change = false
end

然后启动服务器:

RELOAD_MYENGINE=1 rails server

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM