Alex
Alex

Reputation: 4507

Rails 3: how to detect if the application is running in server mode for multiple different environment?

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

Upvotes: 7

Views: 1521

Answers (2)

Jordan Brough
Jordan Brough

Reputation: 7195

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...

Upvotes: 9

bonyiii
bonyiii

Reputation: 2883

What about?

config.serve_static_assets = ( defined?(Mongrel) || defined?(WEBrick) ) ? true : false

Upvotes: 2

Related Questions