rogueg
rogueg

Reputation: 303

Determine if rails is running under a web server

How do I tell if rails is running under a web server, as opposed to a script or irb?

For context: I'm tracking an ActiveRecord object, and I want to email alerts if the objects changes, but only if the changes came through the web interface.

Upvotes: 8

Views: 2210

Answers (3)

Alex Fortuna
Alex Fortuna

Reputation: 1243

I have the task of my application performing several startup checks prior to starting up specifically as a server. In my particular case I want to make sure that a special User record exists in the database since API calls made to my application require this User.

I use the following hack in one of the initializers responsible for startup checks:

if defined? Rails::Server
  puts "Running as a server..."
  # Perform checks here...
end

Upvotes: 7

rogueg
rogueg

Reputation: 303

I've found something that works, though it's not very pretty

$0  # => "irb" (for script/console) 
    # => "/some/handler.fcgi" (for webserver)
    # => "/some/script.rb" (for a script)

Upvotes: -1

bjg
bjg

Reputation: 7477

Perhaps I'm missing something obvious but isn't the fact that if your model access occurs through a controller interface (i.e. some action) then, by definition, an access through a web interface?

Rails doesn't run under irb. Yes, you can access objects and state under the console but that's not a web server access.

I think you just need to set up some filters in your controllers to implement your tracking logic for the models of interest and then you can differentiate between general access and web-server access (which is controller initiated).

Make sense?

Upvotes: 0

Related Questions