Problem restarting Unicorn

Andrew Stewart boss at airbladesoftware.com
Thu Apr 5 07:21:01 UTC 2012


Hello,

I seem to be confused about how to restart Unicorn when deploying updates to a Rails app via Capistrano.

I just had a problem where a bundled gem wasn't being seen by the Unicorn processes, even though app code I introduced at the same time was being served by the Unicorn processes.  I had restarted Unicorn numerous times using `bundle exec cap deploy:restart` (see the code below), which I thought was the right way to do zero-downtime restarts, but my problem was only solved by doing a hard restart of Unicorn with `bundle exec cap deploy:hard_restart`.

With each "normal" restart I tailed the stderr log to check everything was ok; everything appeared fine, with gems refreshed etc.

Please could somebody explain what I'm doing wrong with my normal restarts?

config/deploy.rb:

    set :unicorn_config, "#{current_path}/config/unicorn.rb"
    set :unicorn_pid,    "#{shared_path}/pids/unicorn.pid"

    namespace :deploy do
      task :start, :roles => :app, :except => { :no_release => true } do
        run "cd #{current_path} && #{sudo_workaround} bundle exec unicorn -c #{unicorn_config} -E #{rails_env} -D"
      end
      task :stop, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill -s QUIT `cat #{unicorn_pid}`"
      end
      task :hard_stop, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill `cat #{unicorn_pid}`"
      end
      task :restart, :roles => :app, :except => { :no_release => true } do
        run "#{sudo_workaround} kill -s USR2 `cat #{unicorn_pid}`"
      end
      task :hard_restart, :roles => :app, :except => { :no_release => true } do
        hard_stop
        start
      end
    end
    
    # Works around Ubuntu changing the PATH when you use sudo.
    # sudo's path can find common commands like echo but not much else.
    def sudo_workaround
      "sudo env PATH=$PATH"
    end


config/unicorn.rb:

    APP_PATH = '/var/www/apps/sparkle'
    worker_processes 4
    user 'rails', 'rails'
    working_directory "#{APP_PATH}/current"
    listen "/tmp/unicorn_sparkle.sock", :backlog => 64
    timeout 30
    pid "#{APP_PATH}/shared/pids/unicorn.pid"
    stderr_path "#{APP_PATH}/shared/log/unicorn.stderr.log"
    stdout_path "#{APP_PATH}/shared/log/unicorn.stdout.log"
    preload_app true
    GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
    
    before_fork do |server, worker|
      defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
    
      old_pid = "#{server.config[:pid]}.oldbin"
      if old_pid != server.pid
        begin
          sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
          Process.kill(sig, File.read(old_pid).to_i)
        rescue Errno::ENOENT, Errno::ESRCH
        end
      end
    end
    
    after_fork do |server, worker|
      defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
    end

This is all with Unicorn 4.2.0 and Rails 3.0.11.

Many thanks in advance,

Andy Stewart
----
http://airbladesoftware.com



More information about the mongrel-unicorn mailing list