gpt4 book ai didi

ruby-on-rails-3.2 - 如何优雅地重启 unicorn ?

转载 作者:行者123 更新时间:2023-12-02 03:45:15 25 4
gpt4 key购买 nike

我有一台服务器,每 1 分钟与厨师一起部署。为了重启 unicorn ,我将 USR2 信号发送给老主人,然后尝试通过代码减少老 worker :

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)

这意味着新的master无法启动(我在进程列表中没有看到new master)但是旧的master无法关闭并且worker的数量是恒定的。

我可能做错了什么?谢谢)

最佳答案

重新启动 unicorn 的优雅方式将是 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 值覆盖到远程生产和测试服务器上。

关于ruby-on-rails-3.2 - 如何优雅地重启 unicorn ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com