ruby-on-railsrubyrakeeventmachine

Detect if application was started as HTTP server or not (rake task, rconsole etc)


I'm using EventMachine and Monetarily to start e TCP server along with my rails application. This is started from config/initializers/momentarily.rb.

My problem is that it starts also when I run rake tasks, like db:migrate. I only want it to start when when I start the HTTP server. Environments won't help, since both the server start and rake tasks are under Development environment. Is there a way of knowing that the application is running the HTTP server as opposed to anything else? Note that is not only rake tasks, the EM starts also if I run the rails console, which is again something not desirable for my case.


Solution

  • There's not a great way of doing this that I know of. You could copy newrelic's approach (check discover_dispatcher in local_environment.rb) which basically has a list of heuristics used to detect if it is running inside passenger, thin, etc.

    For passenger it checks

    defined?(::PhusionPassenger)
    

    for thin it checks

    if defined?(::Thin) && defined?(::Thin::Server)