Search code examples
ruby-on-railsunicorn

Rails: Is it possible to auto adjust unicorn worker processes quantity?


I have a rails app running on unicorn & NGINX app&web server.

Whenever the system is booted, unicorn is started with the required number of workers which is defined in the config/unicorn.rb file.

Whenever I depoly my rails app to a different CPU&Memory configuration server I need to manually change unicorn.rb and then manually restart unicorn.

Is it possible to define number of workers for unicorn more dynamically, such as unicorn starts with number of workers suitable for the HW configuration of the server? (ie: different number of unicorn workers for different number of CPUs in the server?)

config/unicorn.rb

worker_processes 4
preload_app true
timeout 30

Solution

  • Unicorn config is ruby dsl, you can write code in it.

    require 'etc'
    
    worker_processes Etc.nprocessors
    preload_app true
    ...