繁体   English   中英

我是否可以在Heroku + Unicorn中正确预加载应用程序?

[英]Am I preloading the app in Heroku + Unicorn correctly?

在Heroku上使用独角兽时。 扩大规模会遇到问题,因为新缩放的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

只有部分答案,但是我可以使用以下Unicorn配置减少这些讨厌的缩放超时:

worker_processes 3 # amount of unicorn workers to spin up
timeout 30         # restarts workers that hang for 30 seconds
preload_app true

# hack: traps the TERM signal, preventing unicorn from receiving it and performing its quick shutdown.
# My signal handler then sends QUIT signal back to itself to trigger the unicorn graceful shutdown
# http://stackoverflow.com/a/9996949/235297
before_fork do |_server, _worker|
  Signal.trap 'TERM' do
    puts 'intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end
end

# Fix PostgreSQL SSL error
# http://stackoverflow.com/a/8513432/235297
after_fork do |server, worker| 
  defined?(ActiveRecord::Base) and 
  ActiveRecord::Base.establish_connection 
end

另外,我使用heroku labs:enable preboot (请参阅https://devcenter.heroku.com/articles/labs-preboot/ )。 不幸的是,扩大网络测功时,我仍然看到一些超时。

这是我发起的HireFire支持论坛中的讨论: http ://hirefireapp.tenderapp.com/discussions/problems/205-scaling-up-and-down-too-quickly-provoking-503s

preload_app true对我们的应用程序有帮助,因此如果您在部署/重新启动期间遇到超时问题,请preload_app true 评论说它无济于事,让我觉得这不值得尝试,然后才意识到这确实是我们需要的解决方案。

我们的情况是使用preboot的启动缓慢的Rails应用程序。 在某些部署和重新启动上,我们将获得大量超时,以至于我们的正常运行时间监控已将该站点视为停机。

我们意识到,使用preload_app false ,Unicorn将首先绑定其端口,然后加载该应用程序。 绑定端口后,Heroku便开始发送流量。 但是,此缓慢的应用程序需要很长的时间才能加载,因此流量会超时。

这很容易验证,方法是在开发人员中运行Unicorn,在启动Unicorn后立即尝试访问该网站,并检查是否收到“该端口上没有服务器”类型的错误(理想)或请求非常慢(不理想)。

当我们改为将preload_app true设置preload_app true ,Unicorn绑定端口将需要更长的时间,但是一旦端口绑定并且Heroku向其发送流量,就可以进行响应了。

您在这里看到的是预期的。 当您通过一个dyno进行扩展时,Heroku平台会将该子弹部署到一个新的dyno,该dyno与您的其他dynos(即另一个独角兽大师)完全隔离。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM