【发布时间】:2012-02-18 22:31:27
【问题描述】:
在 Heroku 上使用 Unicorn 时。扩大规模会有问题,因为新扩大的网络测功机可以在仍在加载应用程序时通过请求访问。这主要导致超时错误。
我在http://codelevy.com/2010/02/09/getting-started-with-unicorn.html 和https://github.com/blog/517-unicorn 做了一些阅读
这两篇文章建议使用preload_app true。还有一个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
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 heroku unicorn