Search code examples
rubyunicorn

Use `reload` instead of `restart` for Unicorn?


I'm a little confused about my deploy strategy here, when deploying under what circumstances would I want to send a reload signal to unicorn? For example in my case it would be like:

sudo kill -s USR2 `cat /home/deploy/apps/my_app/current/tmp/pids/unicorn.pid`

I've been deploying my apps by killing that pid, then starting unicorn again via something like:

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

I'm just wondering why I'd want to use reload? Can I gain any performance for my deployment by doing so?


Solution

  • When you kill unicorn you cause downtime, until unicorn can start back up. When you use the USR2 signal, unicorn starts new workers first, then once they are running, it kills the old workers. It's basically all about removing the need to "turn off" unicorn.

    Note, the assumption is that you have the documented before_fork hook in your unicorn configuration, in order to handle the killing of the old workers, should an ".oldbin" file be found, containing the PID of the old unicorn process:

    before_fork do |server, worker|
      # a .oldbin file exists if unicorn was gracefully restarted with a USR2 signal
      # we should terminate the old process now that we're up and running
      old_pid = "#{pids_dir}/unicorn.pid.oldbin"
      if File.exists?(old_pid)
        begin
          Process.kill("QUIT", File.read(old_pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
          # someone else did our job for us
        end
      end
    end