Rails 3.1:如何仅为 Web 应用程序运行初始化程序(rails 服务器/unicorn/等)

发布于 2024-12-05 17:37:59 字数 943 浏览 1 评论 0原文

我的网络应用程序需要加密其会话数据。我的设置是:

config/initializers/encryptor.rb:

require 'openssl'
require 'myapp/encryptor'

MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 )
Session.delete_all

app/models/session.rb:

require 'attr_encrypted'

class Session < ActiveRecord::Base
  attr_accessible :session_id, :data
  attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true

  # Rest of model stuff
end

一切都很好,并且可以保证会话数据的安全。问题是:当我运行自定义 rake 任务时,它会加载初始化程序并清除所有会话。不好!

我可以在初始化程序中放入什么来确保它只为 web 应用程序初始化运行?或者,我可以在初始化程序中放入什么以使其不运行 rake 任务?

更新:好的,我现在所做的是添加MYAPP_IN_RAKE = true,除非定义? MYAPP_IN_RAKE 到我的 .rake 文件。然后在我的初始化程序中我这样做:

unless defined?( MYAPP_IN_RAKE ) && MYAPP_IN_RAKE
    # Web only initialization
end

似乎有效。但我愿意接受其他建议。

My webapp needs to encrypt its session data. What I setup is:

config/initializers/encryptor.rb:

require 'openssl'
require 'myapp/encryptor'

MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 )
Session.delete_all

app/models/session.rb:

require 'attr_encrypted'

class Session < ActiveRecord::Base
  attr_accessible :session_id, :data
  attr_encryptor :data, :key => proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true

  # Rest of model stuff
end

That all works great, and keeps the session data secured. Here's the problem: when I run my custom rake tasks it loads the initializer and clears all the sessions. Not good!

What can I put in my initializer to make sure it ONLY runs for the webapp initialization? Or, what can I put in my initializer to make it NOT run for rake tasks?

Update: OK, what I've done for the moment is add MYAPP_IN_RAKE = true unless defined? MYAPP_IN_RAKE to my .rake file. And then in my initializer I do:

unless defined?( MYAPP_IN_RAKE ) && MYAPP_IN_RAKE
    # Web only initialization
end

Seems to work. But I'm open to other suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-12-12 17:37:59

您可以像这样在`config/application.rb'中对您的应用程序进行修改:

module MyApp
  def self.rake?
    !!@rake
  end

  def self.rake=(value)
    @rake = !!value
  end

然后在您的Rakefile中添加以下内容:

MyApp.rake = true

使用方法而不是常量是很好的,因为有时您更喜欢稍后更改或重新定义它们。另外,它们不会污染根命名空间。

下面是一个 config/initializers/rake_environment_test.rb 脚本示例:

if (MyApp.rake?)
  puts "In rake"
else
  puts "Not in rake"
end

Rakefile 的可编程特性为您提供了显着的灵活性。

You might make a modification to your application in `config/application.rb' like this:

module MyApp
  def self.rake?
    !!@rake
  end

  def self.rake=(value)
    @rake = !!value
  end

Then in your Rakefile you'd add this:

MyApp.rake = true

It's nice to use methods rather than constants since sometimes you'd prefer to change or redefine them later. Plus, they don't pollute the root namespace.

Here's a sample config/initializers/rake_environment_test.rb script:

if (MyApp.rake?)
  puts "In rake"
else
  puts "Not in rake"
end

The programmable nature of the Rakefile affords you significant flexibility.

甜味超标? 2024-12-12 17:37:59

还有另一种解决方法:

unless ENV["RAILS_ENV"].nil? || ENV["RAILS_ENV"] == 'test'

当您使用 rake 启动时,您的 ENV["RAILS_ENV"] 将为零。 “test”的测试是为了避免在使用 rspec 时运行。

我知道应该使用 Rails.env,但在未初始化时它会返回“development”。

http://apidock.com/rails/Rails/env/class

# File railties/lib/rails.rb, line 55
def env
  @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] 
     || ENV["RACK_ENV"] || "development")
end

There is another work around:

unless ENV["RAILS_ENV"].nil? || ENV["RAILS_ENV"] == 'test'

When you launch with rake your ENV["RAILS_ENV"] will be nil. The test for 'test' is to avoid to run when using rspec.

I know that is reckon to use Rails.env but it return "development" when it is not initialised.

http://apidock.com/rails/Rails/env/class

# File railties/lib/rails.rb, line 55
def env
  @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] 
     || ENV["RACK_ENV"] || "development")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文