Unicorn零停机不起作用

时间:2013-11-21 08:18:48

标签: ruby-on-rails capistrano unicorn

我正在使用capistrano 3.虽然-USR2信号似乎没有重新加载应用程序,但是unicorn重启。

#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

当我运行cap production deploy unicorn成功重启时,似乎没有重新加载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 - 不适合我

1 个答案:

答案 0 :(得分:1)

通过以下更改解决问题:

# 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

另请参阅:http://unicorn.bogomips.org/Sandbox.html