簡體   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