简体   繁体   English

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

[英]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_pathsautoload_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”文件夹内的文件夹中开发插件:

  • app 应用
    • models 楷模
    • controllers 控制器
    • helpers 助手
    • views 意见
    • your_plugin_here your_plugin_here

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.

相关问题 如何在开发模式下强制Rails 3.2按请求重新加载已安装的机架应用程序? - How can I force Rails 3.2 to reload mounted rack app per request in development mode? 如何在 Rails 的开发模式下为每个请求自动重新加载 gem 代码? - How can I automatically reload gem code on each request in development mode in Rails? 如何强制ActiveRecord重新加载一个类? - How do I force ActiveRecord to reload a class? Rails 和 MathJax 上的 Ruby - 如何强制页面重新加载,以便我的 mathjax 将呈现? - Ruby on Rails & MathJax - how do I force page reload so my mathjax will render? 我可以强制Rails会话从字符串重新加载吗? - Can I force a rails session to reload from a string? 如何使用每个ajax请求更新实例变量? - How can I update an instance variable with each ajax request? 如何在每个请求中修改ActiveAdmin中的“标题”部分? - How can I modify the “header” section in ActiveAdmin, in each request? 如何在Rails插件中添加控制器和视图 - How can I add my controllers and views in a plugin for rails 如何生成与插件相关的文档? - How can I generate documentation related to my plugin? 如何在使用分块请求时强制关闭excon连接 - How can I force close excon connection when using chunked request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM