开发者

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 开发者_如何学Pythonthe changes came through the web interface.


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


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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜