在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