How can my Rails code tell whether it is running in a server (e.g. thin) or in rspec?

432 views Asked by At

It turns out that the thin server is very particular about starting the EventMachine reactor for itself. That would be fine except that there are circumstances where I need to start the reactor because the process is not being run inside of thin.

So the simple question is how can I programatically determine that my Rails application is being started by a thin server? In that case I won't start my own EventMachine reactor, otherwise I have to.

1

There are 1 answers

1
leei On

After walking through both Rails and Thin initialization, it appears that there really is no penetration into the Application instance from Thin::Server. That seems to mean that I can't examine my app in order to see the server running it.

So I simply opted to check whether Thin::Server was defined. Since I have thin loaded with:

gem 'thin', require: false

I'm only going to have the Thin::Server class initialized if the application is being started from thin. I've checked the rails console, rake tasks and delayed_jobs workers and the assumption seems to hold.

So, in my application.rb:

# Start Faye...
config.middleware.delete Rack::Lock

thin_server = defined?(Thin::Server)

config.after_initialize do |app|
  Faye.logger = Rails.logger

  unless thin_server
    Faye.logger.debug "Ensure reactor running!"
    Faye.ensure_reactor_running!
  end
end

faye_params = {mount: '/faye', timeout: 25}
faye_params[:server] = 'thin' if thin_server

config.middleware.use FayeRails::Middleware, faye_params