Rails 3:如何检测应用程序是否在服务器模式下运行多个不同的环境? [英] Rails 3: how to detect if the application is running in server mode for multiple different environment?

查看:113
本文介绍了Rails 3:如何检测应用程序是否在服务器模式下运行多个不同的环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在多台服务器上运行的应用程序:
- 在开发机器上本地
- 在heroku
- 在带有Nginx Passanger的特定服务器上



我试图启动一个特殊的代码(加载一些REDIS密钥),只有在启动web服务器时才需要。



我已经完成我发现最好的解决方案是在初始化器中执行我的代码:

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

这适用于本地,但似乎Rails :: Server永远不会在Heroku或Passanger中定义。



我需要一个适用于任何情况的解决方案,请帮助,这非常重要。



谢谢,

Alex



ps :我正在运行Rails 3.0.4,Ruby 1.8.7

解决方案

将代码放入您的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...

这篇关于Rails 3:如何检测应用程序是否在服务器模式下运行多个不同的环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆