ruby-on-rails - 我是否正确地在 Heroku + Unicorn 中预加载了应用程序?

标签 ruby-on-rails ruby-on-rails-3 heroku unicorn

在 Heroku 上使用 Unicorn 时。向上扩展会出现问题,因为当新扩展的 Web dyno 仍在加载应用程序时可以通过请求访问它。这主要导致超时错误。

我在 http://codelevy.com/2010/02/09/getting-started-with-unicorn.html 做了一些阅读和 https://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 平台会将该 slug 部署到一个新的 dyno,该 dyno 与您的其他 dyno(即另一个 unicorn 大师)完全隔离。

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

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

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

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

相关文章:

ruby-on-rails - 回滚所有 rails 迁移或删除表并修改迁移(从头开始)

ruby-on-rails-3 - 每当 gem 不运行时的 cron

ruby-on-rails-3 - Phusion Passenger + 带 Rails 3 的 NGinx

node.js - Dockerized MongoDB 无法从 NodeJs 容器连接到 Heroku

reactjs - Heroku 提供 create-react-app 开发构建而不是生产

javascript - AngularJS 到 Rails MassAssignmentSecurity::Error

ruby-on-rails - 尝试使用 SSL 运行 Rails 4 应用程序时出现 403/Forbidden 错误

ruby-on-rails - 如何找到使用特定 View 的位置?

ruby-on-rails - Active Record 获取 PG::InsufficientPrivilege: ERROR: 当用户权限退出时

ruby-on-rails - 为什么这个查询在 Heroku 上不起作用?