How to determine if rails is running under Unicorn?

I need to configure the connection to an external service in my Rails application. I do this in the initializer. The problem is that the service library uses streaming delivery (which I need because I can’t miss it), but the unicorn’s life cycle causes the stream to kill and workers never see it. One solution is to call a new connection for each request, but this is unnecessarily wasteful.

The best solution is to configure the connection in the after_fork block in the unicorn configuration. The problem is that it is not called outside the unicorn, which means that we cannot test it in development / testing environments.

So the question is, what is the best way to determine if a Rails application is running under Unicorn (both the wizard and the workflow)?

+4
source share
5 answers

There is an environment variable available in Rails (I know that it exists in versions 3.0 and 3.1), check the value of env['SERVER_SOFTWARE'] . You can simply put a comparison of regular expressions or strings with this value to determine which server you are running on.

I have a template in my admin that goes through the env variable and spits out its contents.

Unicorn 4.0.1

 env['SERVER_SOFTWARE'] => "Unicorn 4.0.1" 

rails server (webrick)

 env['SERVER_SOFTWARE'] => "WEBrick/1.3.1 (Ruby/1.9.3/2011-10-30)" 
+3
source

You can check defined?(Unicorn) in your Gemfile: gem :unicorn, require: false . Actually, you don’t need the Unicorn library loaded in your rails application. The server is started by the unicorn command from the shell

+3
source

Checking the Unicorn constant seems like a good solution, BUT , it depends a lot on whether require: false is Gemfile in the Gemfile . If this is not the case (which is likely), the check may give a false result.

I solved it very simply:

 # `config/unicorn.rb` (or alike): ENV["UNICORN"] = 1 ... # `config/environments/development.rb` (or alike): ... # Log to stdout if Web server is Unicorn. if ENV["UNICORN"].to_i > 0 config.logger = Logger.new(STDOUT) end 

Hooray!

+1
source

You can check if the Unicorn module has been defined using Object.constants.include?('Unicorn') .

This is very specific to the Unicorn, of course. A more general approach would be to have a method that establishes your connection and remembers that it is already done. If it is called multiple times, it simply does nothing on subsequent calls. Then you call the method in after_fork and in before_filter in your application controller. If it was run in the after_fork file, it does nothing in the before_filter file, if it is not already running, it does its job on the first request and does nothing on subsequent requests.

0
source

Inside config/unicorn.rb Define the ENV variable as

ENV['RAILS_STDOUT_LOG']='1' worker_processes 3 timeout 90

and then this ENV['RAILS_STDOUT_LOG'] variable will be available anywhere in the Rails application workflow.

my problem: I wanted to display all the logs (SQL queries) when on Unicorn workers, and not on other Heroku workers, so I added the addition of the env variable to the unicorn configuration file

0
source

Source: https://habr.com/ru/post/1380306/


All Articles