При использовании Unicorn на Heroku. При увеличении масштаба возникнут проблемы, поскольку к недавно масштабируемому веб-дино можно получить доступ по запросу, когда он все еще загружает приложение. Что в основном приводит к ошибке тайм-аута.
Я немного прочитал на 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