微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我在http://codelevy.com/2010/02/09/getting-started-with-unicorn.htmlhttps://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

解决方法

在这里看到的是预期的.在你通过dyno扩展的那一刻,Heroku平台将把那个slu to部署到一个新的dyno,它与你的其他dynos(即另一个独角兽大师)完全隔离.

一旦该dyno被部署并运行(有效启动),路由网格将开始向该dyno发送请求,这是Rails将在Unicorn或其他任何服务器上启动时启动.

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

因此,总而言之,您的问题与分叉无关,而是您的应用程序无法在30秒内启动,因此早期超时.担心分叉和PID文件不是你需要在Heroku平台上担心的事情.

原文地址:https://www.jb51.cc/ruby/267981.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐