在Heroku上使用 unicorn 时。扩大规模会遇到问题,因为新缩放的Web dyno可以在仍加载应用程序时被请求访问。多数情况下会导致超时错误。

我在http://codelevy.com/2010/02/09/getting-started-with-unicorn.htmlhttps://github.com/blog/517-unicorn上做了一些阅读

这两篇文章建议使用preload_app true。还有一个after_forkbefore_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进行扩展时,Heroku平台会将该子弹部署到一个新的dyno,该dyno与您的其他dynos(即另一个 unicorn 大师)完全隔离。

一旦该dyno部署并运行(有效启动),路由网格将开始向该dyno发送请求,这是Rails在Unicorn或任何您已设置的服务器上启动的时间。

但是,一旦该请求到达,您将有30秒的窗口返回数据,否则该请求将在路由网格上超时(错误H12)。

因此,总而言之,您的问题与 fork 无关,这是因为您的应用程序无法在30秒内启动,因此提前超时。在Heroku平台上,您无需担心 fork 和PID文件。

关于ruby-on-rails - 我是否可以在Heroku + Unicorn中正确预加载应用程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9344788/

10-11 01:36