[英]How can I force my plugin to reload with each request?
As I understand it, plugins are not reloaded in Rails with each request in development mode. 据我了解,在开发模式下每个请求都不会在Rails中重新加载插件。 Which makes sense, as generally you add plugins to your app and it's the app you're developing.
这是有道理的,因为通常您将插件添加到您的应用程序,它是您正在开发的应用程序。
But if you are developing a plugin, you have to restart the server with each change to the plugin which has a significant overhead. 但是如果你正在开发一个插件,你必须重新启动服务器,每次更改插件都会产生很大的开销。
Is there any way to make Rails reload your plugin during development, the way it reloads your models and controllers? 有没有办法让Rails在开发过程中重新加载你的插件,重新加载模型和控制器的方式?
I have been struggling with this for some time, too. 我也一直在努力解决这个问题。 None of the solutions work, including the
autoload_paths
and autoload_once_paths
tricks. 这些解决方案都不起作用,包括
autoload_paths
和autoload_once_paths
技巧。 Furthermore, the hack with FileUpdateChecker
and initializers also does not help (the checker triggers properly, but the files are not reloaded). 此外,使用
FileUpdateChecker
和初始化程序的hack也无济于事(检查程序正确触发,但文件未重新加载)。 Same for config.reload_plugins = true
... config.reload_plugins = true
相同...
However, there is a solution. 但是,有一个解决方案。 In
app/controllers/application_controller.rb
add one line: require_dependency 'your_file_name_here'
The application controller is reloaded on every request and require_dependency
makes your file to be checked for modifications and reloaded accordingly. 在
app/controllers/application_controller.rb
添加一行: require_dependency 'your_file_name_here'
应用控制器被重新加载在每次请求和require_dependency
使得用于修改要被检查,并相应地重新加载文件。 It worked for me, Apache 2, Passenger 3 and Rails 3.0.3. 它适用于我,Apache 2,Passenger 3和Rails 3.0.3。
I do this by using the shotgun gem. 我是通过使用猎枪宝石来做到这一点的。
gem install shotgun
cd /path/to/rails/app
shotgun
slower response time, but reloading all the rails code, not wasting time to write autoload_paths 响应时间较慢,但重新加载所有rails代码,而不是浪费时间编写autoload_paths
Easy way, develop your plugin in a folder inside the "app" folder: 简单的方法,在“app”文件夹内的文件夹中开发插件:
This way, all your plugin classes will be reloaded on each request. 这样,您的所有插件类都将在每个请求上重新加载。
Another possibility is to add the path at your application.rb file: 另一种可能性是在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
This way your plugin is going to be reloaded all the time. 这样你的插件就会一直重新加载。
If restarting the server automatically when plugin code changes is an acceptable solution, you could use Mike Clark/topfunky's rstakeout
for that, or the newer watchr
which sounds like it does the same thing. 如果在插件代码更改时自动重新启动服务器是一个可接受的解决方案,那么您可以使用Mike Clark / topfunky的
rstakeout
,或者更新的watchr
,它听起来像是做同样的事情。
You would do something like this: 你会做这样的事情:
rstakeout 'touch tmp/restart.txt' 'vendor/plugins/**/*'
With https://github.com/ranmocy/guard-rails , it's super easy: 使用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
This solution, for engines, works on Rails 2.3 but comes with one cavaet, it will load all files in the engine and parent app on every request which will make response times slower. 这个解决方案适用于引擎,适用于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
Then start the server: 然后启动服务器:
RELOAD_MYENGINE=1 rails server
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.