Unicorn - 使用HUP零停机时间?

时间:2013-02-24 22:45:54

标签: ruby-on-rails ruby unicorn

我对使用unicorn实现零停机所需的内容感到困惑,更具体地说,如果它与preload_app选项无关。我知道如果我有preload_app false,我可以简单地发送一个HUP信号,而unicorn会自动考虑新的代码,但它是否会以零时间方式进行?

此外,如果内存不是问题,我是否需要使用preload_app为真?

最后,我看到很多示例,其中有一个很大的前fork块,其中包含有关oldpid的代码。该代码何时需要?

谢谢

1 个答案:

答案 0 :(得分:0)

我设置它的方法是在重新启动unicorn服务器时使用kill -USR2 $(cat /path/to/unicorn.pid)之类的东西,在我的unicorn服务器配置中使用类似的东西(基于http://unicorn.bogomips.org/examples/unicorn.conf.rb):

# feel free to point this anywhere accessible on the filesystem
pid "#{shared_path}/pids/unicorn.pid"
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 (especially in the "preload_app false" case)
  # 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
end

这将启动新工人并逐渐关闭旧工人。