简体   繁体   English

关于我的heroku rails app上的unicorn :: clientshutdown错误,我该怎么办?

[英]What can I do about unicorn::clientshutdown errors on my heroku rails app?

I have an app that accepts image uploads. 我有一个接受图片上传的应用程序。 It's a rails 3 app on heroku using Unicorn. 这是使用Unicorn的heroku上的rails 3应用程序。 I'm occasionally getting unicorn::clientshutdown exceptions, and I don't really know what causes them or how to handle them. 我偶尔会得到unicorn::clientshutdown例外,我真的不知道是什么原因导致它们或如何处理它们。 What should I do? 我该怎么办?

This is my unicorn.rb file: 这是我的unicorn.rb文件:

before_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info('Disconnected from Redis')
  end
end

after_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info('Connected to ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis = ENV['REDIS_URI']
    Rails.logger.info('Connected to Redis')
  end
end

Image uploads, Heroku, and Unicorn, oh my. 图片上传,Heroku和Unicorn,哦,我的。

Problem 问题

This is the trifecta for this error. 这是此错误的三连胜。 There is probably a correlating H12 error ( https://devcenter.heroku.com/articles/error-codes#h12-request-timeout ) in your Heroku logs. 您的Heroku日志中可能存在相关的H12错误( https://devcenter.heroku.com/articles/error-codes#h12-request-timeout )。 What is happening is that the request is taking too long to complete (Heroku has an inescapable 30 second timeout), so it disconnected and that unicorn worker was killed. 发生的事情是请求花了太长时间才完成(Heroku有一个不可避免的30秒超时),所以它断开连接并且那个独角兽工人被杀了。 Also, Unicorn isn't great with slow/long-running requests (see http://rainbows.rubyforge.org ) 此外,对于慢速/长时间运行的请求,Unicorn不是很好(参见http://rainbows.rubyforge.org

Solution

The trick is to upload the image on the front-end without hitting the server (CORS/AJAX/jquery.fileupload.js/etc), passing that uploaded file location along with the form submission, then performing any processing later as a background job and reuploading, which isn't subject to the 30 second timeout limit. 诀窍是在前端上传图像而不点击服务器(CORS / AJAX / jquery.fileupload.js / etc),将上传的文件位置与表单提交一起传递,然后再作为后台作业执行任何处理和重新上载,不受30秒超时限制。 Others have written more extensively about this. 其他人则对此进行了更广泛的撰写。 Also, you could use a service like Cloudinary to do this for you. 此外,您可以使用Cloudinary等服务为您执行此操作。

PS PS

YMMV, but you should add this to your unicorn config as well( https://devcenter.heroku.com/articles/rails-unicorn ) YMMV,但你也应该将它添加到你的独角兽配置中( https://devcenter.heroku.com/articles/rails-unicorn

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end
  # ...
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
  end
  # ...
end

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

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