Question

I'm using capistrano 3. And unicorn restart though -USR2 signal seems does not reload app.

#config/unicorn.rb
rails_root = "/home/deployer/apps/my_app/current"
worker_count = 2
worker_processes worker_count

working_directory rails_root

worker_count.times do |index|
  listen "#{rails_root}/tmp/sockets/unicorn.sock.#{index}", :backlog => 64
end

timeout 30

pid "#{rails_root}/tmp/pids/unicorn.pid"

stderr_path rails_root + "/log/unicorn.stderr.log"
stdout_path rails_root + "/log/unicorn.stdout.log"

preload_app true

before_fork do |server, worker|
  old_pid = "#{rails_root}/tmp/pids/unicorn.pid.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      server.logger.info("sending QUIT to #{old_pid}")
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
end


#unicorn.cap
namespace :deploy do

  desc 'Restart application'
  task :restart do
    sleep 3
    invoke 'unicorn:restart'
  end
end

namespace :unicorn do
  task :show_vars do
    on roles :app do
      puts <<-EOF.gsub(/^ +/, '')
        unicorn_pid     #{fetch(:unicorn_pid)}
        deploy_to     #{fetch(:deploy_to)}
        deploy_path     #{fetch(:deploy_path)}
      EOF
    end
  end


  def run_unicorn
    within current_path do
      execute :unicorn_rails, "-c #{current_path}/config/unicorn.rb -D -E #{fetch(:rails_env)}"
    end
  end

  desc "Start Unicorn"
  task :start do
    on roles :app do
      run_unicorn
    end
  end

  desc 'Stop unicorn'
  task :stop do #, :except => { :no_release => true }
    on roles(:app) do
      if test "[ -f #{fetch(:unicorn_pid)} ]"
        execute :kill, "-s QUIT `cat #{fetch(:unicorn_pid)}`"
      end
    end
  end

  desc 'Restart unicorn'
  task :restart do #, except => { :no_release => true }
    on roles(:app) do
      if test "[ -f #{fetch(:unicorn_pid)} ]"
        execute :kill, "-USR2 `cat #{fetch(:unicorn_pid)}`"
      else
        run_unicorn
      end
    end
  end
end

When I run cap production deploy unicorn successfully restarting, but seems does not reload app.

#before deploy
root@sandbox:~# ps aux | grep unicorn
deployer 13418  0.0  1.9 363516 120064 ?       Sl   00:20   0:23 unicorn_rails master -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
deployer 13426  0.0  2.0 369828 123332 ?       Sl   00:21   0:02 unicorn_rails worker[0] -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
deployer 13429  0.0  2.0 374580 127884 ?       Sl   00:21   0:03 unicorn_rails worker[1] -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
root     17250  0.0  0.0   7828   884 pts/1    R+   11:36   0:00 grep unicorn

#after deploy
root@sandbox:~# ps aux | grep unicorn
deployer 17300 16.2  1.9 298320 120288 ?       Sl   11:37   0:22 unicorn_rails master -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
deployer 17319  1.3  2.0 305496 124100 ?       Sl   11:37   0:01 unicorn_rails worker[0] -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
deployer 17322  1.2  2.0 309064 127620 ?       Sl   11:37   0:01 unicorn_rails worker[1] -c /home/deployer/apps/my_app/current/config/unicorn.rb -D -E production
root     17330  0.0  0.0   7828   880 pts/1    R+   11:39   0:00 grep unicorn

Restarting Unicorn with USR2 doesn't seem to reload production.rb settings - don't work for me

Was it helpful?

Solution

Solved problem by following changes:

# config/unicorn.rb
Unicorn::HttpServer::START_CTX[0] = "#{rails_root}/bin/unicorn"

before_exec do |server|
  # Ensure unicorn picks up our newest Gemfile
  ENV['BUNDLE_GEMFILE'] = "<%= current_path %>/Gemfile"
end

# unicorn.cap 
def run_unicorn
  within current_path do
    # change execute :unicorn, instead of :unicorn_rails
    execute :unicorn, "-c #{current_path}/config/unicorn.rb -D -E #{fetch(:rails_env)}"
  end
end

See also: http://unicorn.bogomips.org/Sandbox.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top