How to Use Unicorn as "Rails S"

How can I use unicorn as rails s?

It looks like the unicorn-rails gem that @Dogbert mentioned can actually be used to make Unicorn the rails server handler.

Simply include gem "unicorn-rails" (and for Rails 4.2.4, gem "rack-handlers") in your Gemfile, run bundle install to install the gem, then you can run:

$ rails server unicorn

Although once unicorn-rails is installed, Unicorn should be the default app server so you could also just run rails server and it should use Unicorn (assuming you don't also have Thin or Mongrel in your Gemfile, in which case they may conflict and you might want to remove the ones you're not using).

Unicorn rails server running always

you need to set up unicorn with apache or nginx. here is a guide for apache + unicorn. with that setup you can start and stop server by starting and stopping apache service.

How to start rails server in production mode using unicorn and config file?

It should be something looks like:

bundle exec unicorn -E production -c config/unicorn.rb

and you should only need unicorn gem

Proper way to use Unicorn with Rails

bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D


unicorn_conf - path to unicorn config

rails_env - rails enviroment

You must use unicorn with normal server like nginx

Rails 4: Specified Unicorn in Procfile, but Webrick is executed

rails server doesn't use your Procfile; that's for foreman. Start your application with foreman instead:

bundle exec foreman start

If you want rails server to use Unicorn as well, you can include the unicorn-rails gem.

Referring to Rails from the Unicorn config file

You need to set preload_app true in your config for the gems like Rails to be accessible from the config file - see here for the documentation.

How to use ruby-debug-ide with unicorn_rails?


  • First you need to install Ruby
    extension

  • Add following gems in your Gemfile:

gem 'debase'
gem 'ruby-debug-base', :platforms => [:jruby, :ruby_18, :mingw_18]
gem 'ruby-debug-base19x', '>= 0.11.30.pre4', :platforms => [:ruby_19, :mingw_19]
gem 'ruby-debug-ide' , "~>0.6.1"

Then you need to let rdebug-ide know you are using unicorn (multi-process app) by providing the --dispatcher-port option. Please take a look at rdebug-ide file to see all the available options.

--dispatcher-port: It is a same port that you will use to run unicorn. In your case 3010.

So it should look like this:

bundle exec rdebug-ide --debug --port 1234 --dispatch-port 3010 -- vendor/bundle/ruby/2.6.0/bin/unicorn -E "develop_against_staging" -p 3010 -c "${PWD}/config/unicorn.rb

Running above command alone wont start the debugging, infact your Unicorn server wont be starting yet. When looking at the logs after running the above command in the terminal window you will notice a message something like this

Fast Debugger (ruby-debug-ide 0.6.1, debase 0.2.4.1, file filtering is supported) listens on 127.0.0.1:1234

The logs telling us rdebug-ide is ready to be connected at port 1234. Create a launch.json file if it is not already created and add this configuration.

{
"version": "0.2.0",
"configurations": [
{
"name": "1234 Listen for rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "1234",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
}
]
}

Once you add the entry, go ahead and click on Play Image here button to start debugging.

Now that your unicorn server is started, if you try to access your application you wont be able to access it because the worker process haven't started yet.

Continue to look at logs carefully you will notice
122: Ide process dispatcher notified about sub-debugger which listens on 34865. This is telling us a new sub-debugging process is started on port 34865. This is a randomly generated port find_free_port.

Note: There will a one port per unicorn-worker.

Once you see the above log add another entry into your launch.json file and copy the newly generated port into the file. Like this

{
"version": "0.2.0",
"configurations": [
{
"name": "1234 Listen for rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "1234",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
},
{
"name": "34865 Listen for sub-rdebug-ide",
"type": "Ruby",
"request": "attach",
"remoteHost": "127.0.0.1",
"remotePort": "34865",
"remoteWorkspaceRoot": "${workspaceRoot}",
"cwd": "${workspaceRoot}"
}
]
}

Once added, select the new configuration and click play button. If you had set number of worker to one only in your unicorn.config file, you should see the the log something like this.
I, [2022-07-13T19:44:26.914412 #122] INFO -- : worker=0 ready. Now put a breakpoint and start using your application, it will break once it reach that code path.

If you have successfully setup everything and got to this point, there will be some gotcha that you may need to deal with.

  • Worker timing out
  • Re-linking to sub-debugger with different random port.
  • ...

This complexity is because of unicorn master-worker design.

Answering this in a bit rush, please let me know if you have any questions. I apologies if I made this more confusion for you.



Related Topics



Leave a reply



Submit