Question

I have a middleware for announcing my application on the local network app using Bonjour, but it's also announcing the service when Rails is invoked from rake or through the console.

I'd like to exclude these cases, and only use the Bonjour middleware when Rails is running as a server.

The middleware configuration accepts a proc to exclude middlewares under certain conditions using a proc:

config.middleware.insert_before ActionDispatch::Static, Rack::SSL, :exclude => proc { |env| 
  env['HTTPS'] != 'on' 
}

But how do I determine if Rails was invoked from the CLI, console or as a server?

Was it helpful?

Solution

Peeking at the Rails module using pry reveals that console invocations can be detected like this:

Rails.const_defined? 'Console'

And server invocations like this:

Rails.const_defined? 'Server'

OTHER TIPS

Super helpful. Thanks @crishoj.

I wanted to examine the Console object more closely for another problem I am working on and found out that the Console constant can be reached with Rails::Console, so another option for checking would be to use:

defined? Rails::Console
defined? Rails::Server

Using Rails 5 with or without an app-server like Puma/Passenger, here are three ways to determine how your app is running:

# We are running in a CLI console
defined?(Rails::Console)

# We are running as a Rack application (including Rails)
caller.any?{|l| l =~ %r{/config.ru/}}

# We are running as a CLI console
caller.any?{|l| l =~ %r{/lib/rake/task.rb:\d+:in `execute'}}

Summary of the environment for each command.

I found the existing answers to be either incomplete, redundant or not exhaustive. So here is a table format of each command and what the resulting environment looks like.

Rails 4.2

| Command                            |  Rails.const_defined?( "Console" )  |  Rails.const_defined?( "Server" )  |               ARGV              |
|------------------------------------|-------------------------------------|------------------------------------|---------------------------------|
| `rake db:migrate:status`           |  false                              |  true                              |  ["db:migrate:status"]          |
| `rails console`                    |  true                               |  true                              |  []                             |
| `rails server`                     |  false                              |  true                              |  []                             |
| `rails g migration new_migration`  |  false                              |  true                              |  ["migration", "new_migration"] |
| `rails r "puts 'Hi'"`              |  false                              |  true                              |  []                             |

You can see that just checking for "Server" being defined as a Rails constant will not catch generators, like rails g migration. You need to check the ARGV to do that.

I hope this helps. I only had immediate access to Rails 4.2 but feel free to add sections for other Rails versions, as well as add any additional commands that need "catching".

'Server' isn't defined when Rails 5 runs under Passenger.

The best solution I've found is a variant of this answer:

if %w(rails rake).include?(File.basename($0))
   <console or runner>
else
   <server>       
end

In our project I had to detect console mode in boot.rb, for that I used:

in_console = (ARGV & ['c', 'console']).any?

Not a fool-proof solution, but good enough for our use case.

Here is my version that detects sidekiq or running server on passenger/puma. Given the previous answers, it is not 100% sure that it would work in all cases (I haven't tested what it's like when running a rails runner or a rake task in general)

@running_app = begin
  if defined?(Rails::Console)
    'Console'
  elsif Sidekiq.server?
    'Worker'
  elsif defined?(::PhusionPassenger) || defined?(Rails::Server) 
    'Server'
  else
    nil # unknown
  end
end

For Padrino:

Console check:

if Padrino::constants.include? :Cli
    #your code
end

Server Check:

if !Padrino::constants.include? :Cli
    #your code
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top