在Heroku上使用独角兽时。扩大规模将会产生问题,因为新的缩放网络动态码可以通过请求访问,当它仍在加载应用程序时。这主要导致Timeout错误。我是否正确地在Heroku + Unicorn中预载应用程序?
我没有使用preload_app true
在http://codelevy.com/2010/02/09/getting-started-with-unicorn.html和https://github.com/blog/517-unicorn
的两篇文章有点阅读建议。和after_fork
和before_fork
块。
在Rails 3+,是before_block
的代码仍然需要?我在某处读过,否则。谁曾经有过这样的经历并愿意分享?
我错过了什么吗?我是否正确预装应用程序?
# config/initializers/unicorn.rb
# Read from:
# http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/
worker_processes 3 # amount of unicorn workers to spin up
timeout 30 # restarts workers that hang for 90 seconds
# Noted from http://codelevy.com/2010/02/09/getting-started-with-unicorn.html
# and https://github.com/blog/517-unicorn
preload_app true
after_fork do |server, worker|
ActiveRecord::Base.establish_connection
end
before_fork do |server, worker|
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = Rails.root + '/tmp/pids/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
嗨尼尔。解决方案是预装应用程序,以防止请求进入,直到dyno(独角兽主机)完全加载应用程序。我关心的是我是否需要'before_fork'块中的代码? – 2012-02-20 09:31:39
你的before_fork将无法实现。正如我之前所说的,问题在于Heroku路由网格将在您的Unicorn启动之前向您发送请求。预加载应用程序不会解决此问题。 – 2012-02-20 11:07:53
如果是这样的话。如何防止在Heroku上旋转/放大新的Web dynos时发生超时错误? – 2012-02-20 14:08:18