12

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.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569

7 Answers7

9
unless File.basename($0) == "rake" && ARGV.include?("db:migrate")
  # you are not in rake db:migrate
end
Tema Bolshakov
  • 1,128
  • 8
  • 14
8

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)
Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
7

Set an environment variable in config.ru file, and use it anywhere in the code to detect if it's executed using a rails server command only.

For e.g.

  • File: config.ru

    ENV['server_mode'] = '1'
    

And using it somewhere as:

  • File: config/environment.rb

    Thread.new { infinite_loop! }.join if ENV['server_mode'] = '1'
    

Reference: Answer

Community
  • 1
  • 1
Vishal
  • 2,161
  • 14
  • 25
0

Maybe you can implement a switch in the initializer based on ARGV?

Something like:

if ARGV.join(' ').match /something/
  # your initializer code here
end
severin
  • 10,148
  • 1
  • 39
  • 40
0

Don't start that other server from an initializer. Create a daemon in script/momentarily and start it from within your app.

Stefan
  • 109,145
  • 14
  • 143
  • 218
0

After your application launches, you could have it shell out to check ps. If ps shows that the HTTP server is running and the running HTTP server has the same pid as your application (check the pid by inspecting $$), then you could launch the TCP server.

Gordon Wilson
  • 26,244
  • 11
  • 57
  • 60
  • Yes it will. When you run rails server `ps aux | grep rails` will contain something like `remus 18454 0.0 0.8 2515572 65968 s000 S+ 11:10AM 0:01.51 /Users/remus/.rvm/rubies/ruby-1.9.3-p125/bin/ruby script/rails server` – Gordon Wilson Sep 01 '12 at 18:13
0

In addition to a great answer by Frederick Cheung above, there can be some other "footprints" in actual process environment. Eg. Phusion Passenger adds certain variables to ENV such as:

PASSENGER_APP_ENV
IN_PASSENGER
PASSENGER_SPAWN_WORK_DIR
PASSENGER_USE_FEEDBACK_FD

Web servers typically can also set SERVER_SOFTWARE variable eg.:

SERVER_SOFTWARE=nginx/1.15.8 Phusion_Passenger/6.0.2
Dharman
  • 30,962
  • 25
  • 85
  • 135
pokrovskyy
  • 437
  • 5
  • 8