且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rails 3:如何检测应用程序是否在服务器模式下运行多个不同的环境?

更新时间:2023-10-16 10:44:04

将代码放入您的config.ru文件可能是更多跨不同的检测服务器模式的强大方法租用类型的服务器(Unicorn / Passenger / Rails :: Server / etc)。

例如,在rails-root / config.ru中:

 #此文件由基于机架的服务器用于启动应用程序。 

#添加这行并在稍后读取值:
ENV ['server_mode'] ='1'

require :: File.expand_path ...


I have an app that runs on multiple servers: - locally on dev machines - on heroku - on a specific server with Passanger on Nginx

I am trying to launch a particular code (loading some REDIS keys) that is only required if the web server is launched.

I have done quite a bit of digging, and the nicest solution I found was to execute my code in an initializer with:

if defined?(Rails::Server)
   #my code
end

This works well locally, but it seems that Rails::Server never gets defined either on Heroku or Passanger.

I need a solution that works in every case, please help, this is really important.

Thanks,

Alex

ps: I am running Rails 3.0.4, Ruby 1.8.7

Putting code in your config.ru file might be a more robust way of detecting server mode across different types of servers (Unicorn/Passenger/Rails::Server/etc).

e.g., in rails-root/config.ru:

# This file is used by Rack-based servers to start the application.

# ADD this line and read the value later:
ENV['server_mode'] = '1'

require ::File.expand_path...