Ruby-on-rails 在 Heroku 中运行任务的 Rails 2.3 样式插件和弃用警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9027403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:42:14  来源:igfitidea点击:

Rails 2.3-style plugins and deprecation warnings running task in Heroku

ruby-on-railsherokuruby-on-rails-plugins

提问by fearless_fool

I'm upgrading to Rails 3.2, and running rake db:migrate gives me several errors of the form:

我正在升级到 Rails 3.2,并且运行 rake db:migrate 给了我几个形式的错误:

DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/01/04/rails-3-2-0-rc2-has-been-released. (called from at /app/Rakefile:7)

弃用警告:您在供应商/插件中有 Rails 2.3 风格的插件!Rails 4.0 中将删除对这些插件的支持。将它们移出并将它们捆绑在您的 Gemfile 中,或者将它们作为 lib/myplugin/* 和 config/initializers/myplugin.rb 折叠到您的应用程序中。有关更多信息,请参阅发行说明:http: //weblog.rubyonrails.org/2012/01/04/rails-3-2-0-rc2-has-been-released。(从 /app/Rakefile:7 调用)

What's perplexing is that my vendor/pluginsdirectory is empty -- is there another plugins directory that it's referencing?

令人困惑的是我的vendor/plugins目录是空的——它是否引用了另一个插件目录?

回答by Jared Beck

Are you using Heroku?

你在使用 Heroku 吗?

Heroku will inject plugins in Rails 3.x applications .. To avoid this injection in Rails 3, include the rails_12factor gem in your application. (Heroku Ruby Support2013-10-26)

Heroku 将在 Rails 3.x 应用程序中注入插件.. 为避免在 Rails 3 中进行这种注入,请在您的应用程序中包含 rails_12factor gem。( Heroku Ruby 支持2013-10-26)

The rails_12factor gem is also required in rails 4.

rails 4 中也需要 rails_12factor gem。

If this gem is not present in your application, you will receive a warning while deploying, and your assets and logs will not be functional. (Rails 4 on Heroku2013-10-26)

如果您的应用程序中不存在此 gem,您将在部署时收到警告,并且您的资产和日志将无法运行。(Heroku 上的 Rails 42013-10-26)

As recently as 2013-08, heroku always injected plugins in rails 3 apps, even apps with the recommended gems. This was an issue with the ruby buildpack, and was fixed by PR 11, merged on 2013-08-06.

就在 2013-08 年,heroku 总是在 rails 3 应用程序中注入插件,甚至是带有推荐 gems 的应用程序。这是 ruby​​ buildpack 的一个问题,由PR 11修复,于 2013-08-06 合并。

回答by kain

You can try

你可以试试

::ActiveSupport::Deprecation.silenced = true

in your production.rbsince it's just noise.

在你,production.rb因为它只是噪音。

回答by Michael Hale

in config/environment.rb add:

在 config/environment.rb 添加:

ActiveSupport::Deprecation.silenced = true 

before initializing rails, like so:

在初始化 rails 之前,像这样:

# Load the rails application                                                                                                                                             
require File.expand_path('../application', __FILE__)

ActiveSupport::Deprecation.silenced = true                                                                                                                               

# Initialize the rails application                                                                                                                                       
MyApp::Application.initialize!

Similarly to disable warnings in rake tasks insert the silencing config near the top of your Rakefile:

与禁用 rake 任务中的警告类似,请在 Rakefile 顶部附近插入静音配置:

# Load the rails application                                                                                                                                             
require File.expand_path('../application', __FILE__)

ActiveSupport::Deprecation.silenced = true                                                                                                                           

# Initialize the rails application                                                                                                                                       
MyApp::Application.initialize!

You can optionally wrap this in a block to only silence in production:

您可以选择将其包装在一个块中以仅在生产中静音:

if ENV['RAILS_ENV'] == "production"
  ActiveSupport::Deprecation.silenced = true
end

回答by yuяi

The best approach I have found is documented here. This is assuming you searched and found this question because you dohave old-style plugins.

我发现的最佳方法记录在此处。这是假设您搜索并找到了这个问题,因为您确实有旧式插件。

I went with the Make it not a gem at allpart, because I needed to be able to turn plugins on/off during my capistrano deployment, based on what flavor of the app I was deploying. Before I used config.plugins to specify what plugin to use. With this approach I'm using a "require" on config.before_configuration instead.

我选择了Make it not a gem at allpart,因为我需要能够在我的 capistrano 部署期间打开/关闭插件,这取决于我部署的应用程序的风格。在我使用 config.plugins 指定要使用的插件之前。通过这种方法,我在 config.before_configuration 上使用了“require”。

回答by skalee

Just put following monkey patch into /lib/silence_heroku_warnings.rb

只需将以下猴子补丁放入 /lib/silence_heroku_warnings.rb

module Rails
  class Plugin < Engine

    alias :not_silenced_initialize :initialize

    def initialize(root)
      ActiveSupport::Deprecation.silence{ self.send :not_silenced_initialize, root }
    end

  end
end

and require it in config/application.rbjust after requiring Rails:

config/application.rb在要求 Rails 之后立即要求它:

require 'rails/all'
require File.expand_path('../../lib/silence_heroku_warnings', __FILE__)

All deprecations from 2.x-style plugins should be silenced. Other deprecations will show up.

2.x 样式插件的所有弃用都应该被静音。其他弃用将出现。

回答by Wolfram Arnold

A cleaner way than just silencing warnings, here is what you can do.

一种更清洁的方法,而不仅仅是消除警告,这就是您可以做的。

For the logger injection you can try to use Heroku's new gemthat Jared Beckmentioned in his reply above.

对于注射记录器,你可以尝试使用Heroku的新宝石贾里德·贝克在提到他的上述答复

What we did instead is this:

我们做的是这样的:

You can inhibit Heroku from injecting its own plugins if you have a directory by the same name in your vendor/pluginsfolder. The folder just needs to exist. Heroku then will not inject its plugin, and if there is no code, Rails won't object with deprecation warnings. We just put a readme file explaining this into:

如果您的vendor/plugins文件夹中有同名目录,您可以禁止 Heroku 注入自己的插件。该文件夹只需要存在。Heroku 将不会注入它的插件,如果没有代码,Rails 不会反对并发出弃用警告。我们只是把解释这个的自述文件放入:

vendor/plugins/rails_log_stdout/readme.md

The purpose of Heroku's injected plugin for logging is to turn on Heroku-style logging (it requires logs to be sent to STDOUT, not to a file). To get that back, we did what I described in this answer. Tweaks to Heroku's default behaviors were needed for Unicorn anyway, so we got two birds in one stone.

Heroku 用于日志记录的注入插件的目的是打开 Heroku 风格的日志记录(它需要将日志发送到 STDOUT,而不是发送到文件)。为了恢复原状,我们做了我在这个答案中描述的事情。无论如何,Unicorn 都需要对 Heroku 的默认行为进行调整,所以我们一举两得。

回答by raidfive

It looks like Heroku has finally addressed this.

看起来 Heroku 终于解决了这个问题。

   Injecting plugin 'rails_log_stdout'
   Injecting plugin 'rails3_serve_static_assets'
   Add 'rails_12factor' gem to your Gemfile to skip plugin injection

回答by Jonathan Métillon

The new way of silencing deprecation notices is:

使弃用通知静音的新方法是:

config.active_support.deprecation = :silence

in your config/environments/production.rbfile.

在您的config/environments/production.rb文件中。