如何优雅地重启独角兽?

时间:2013-07-03 07:29:45

标签: ruby-on-rails-3.2 unicorn

我有一台服务器,每1分钟与厨师一起部署。 对于重启独角兽,我将USR2信号发送给旧主人,然后尝试通过代码减少旧工人:

before_fork do |server, worker|

  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

  # This allows a new master process to incrementally
  # phase out the old master process with SIGTTOU to avoid a
  # thundering herd when doing a transparent upgrade. The last worker
  # spawned will then kill off the old master process with a SIGQUIT.
  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

  # sleep 1
end

最后,将QUIT信号发送给旧主人

每次我在日志中收到以下消息:

E, [2013-07-03T10:56:19.983813 #19955] ERROR -- : retrying in 0.5 seconds (1 tries left)
E, [2013-07-03T10:56:20.484468 #19955] ERROR -- : adding listener failed addr=0.0.0.0:3000 (in use)
E, [2013-07-03T10:56:20.484595 #19955] ERROR -- : retrying in 0.5 seconds (0 tries left)
E, [2013-07-03T10:56:20.985190 #19955] ERROR -- : adding listener failed addr=0.0.0.0:3000 (in use)                   
/my_path/766ea02ce174c37de606c1960c498d53c5fb602b/vendor/bundle/ruby/1.9.1/gems/unicorn-4.4.0/lib/unicorn/socket_helper.rb:147:in `initialize': Address  already in use - bind(2) (Errno::EADDRINUSE) 

这意味着新的主人无法启动(我没有看到新的主人在进程列表中)但是老主人无法关闭,工人数量不变。

我可能以错误的方式做什么? 感谢)

2 个答案:

答案 0 :(得分:1)

重新启动独角兽的优雅方式将是Rakefile中的rake任务:

task :restart_server => :environment do
     system("kill -QUIT `cat pids/unicorn.pid`")
     system("bundle exec unicorn_rails -c ./config/unicorn.rb -D")
end

这里假设文件“unicorn.pid”的路径,主服务器进程存在于application_root / pids / unicorn.pid中。可以通过更改config / unicorn.rb中的pid值来更改此路径

pid "pids/unicorn.pid"

此外,如果您使用cap进行部署,请确保此文件具有远程服务器的写入权限,并将此文件添加到.gitignore,以便此文件不会将本地pid值覆盖到远程生产和测试服务器上。

答案 1 :(得分:0)

我做了两个步骤来解决这个问题:

1)从unicorn.conf.rb中删除了application_root 2)将最大工人数量从32个设置为16

然后错误从日志中消失了。