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

时间:2022-01-26 20:05:45

I have an app that accepts image uploads. It's a rails 3 app on heroku using Unicorn. I'm occasionally getting unicorn::clientshutdown exceptions, and I don't really know what causes them or how to handle them. What should I do?

我有一个接受图片上传的应用程序。这是使用Unicorn的heroku上的rails 3应用程序。我偶尔会得到unicorn :: clientshutdown异常,我真的不知道它们是什么原因或如何处理它们。我该怎么办?

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

1 个解决方案

#1


4  

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. 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. Also, Unicorn isn't great with slow/long-running requests (see http://rainbows.rubyforge.org)

这是此错误的三连胜。您的Heroku日志中可能存在相关的H12错误(https://devcenter.heroku.com/articles/error-codes#h12-request-timeout)。发生的事情是请求花了太长时间才完成(Heroku有一个不可避免的30秒超时),所以它断开连接并且那个独角兽工人被杀了。此外,对于慢速/长期运行的请求,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. Others have written more extensively about this. Also, you could use a service like Cloudinary to do this for you.

诀窍是在没有命中服务器(CORS / AJAX / jquery.fileupload.js / etc)的情况下在前端上传图像,将上传的文件位置与表单提交一起传递,然后在后面执行任何处理作为后台作业和重新上传,不受30秒超时限制。其他人则对此进行了更广泛的撰写。此外,您可以使用像Cloudinary这样的服务为您执行此操作。

PS

PS

YMMV, but you should add this to your unicorn config as well( https://devcenter.heroku.com/articles/rails-unicorn)

YMMV,但你也应该将它添加到你的unicorn配置中(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

#1


4  

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. 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. Also, Unicorn isn't great with slow/long-running requests (see http://rainbows.rubyforge.org)

这是此错误的三连胜。您的Heroku日志中可能存在相关的H12错误(https://devcenter.heroku.com/articles/error-codes#h12-request-timeout)。发生的事情是请求花了太长时间才完成(Heroku有一个不可避免的30秒超时),所以它断开连接并且那个独角兽工人被杀了。此外,对于慢速/长期运行的请求,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. Others have written more extensively about this. Also, you could use a service like Cloudinary to do this for you.

诀窍是在没有命中服务器(CORS / AJAX / jquery.fileupload.js / etc)的情况下在前端上传图像,将上传的文件位置与表单提交一起传递,然后在后面执行任何处理作为后台作业和重新上传,不受30秒超时限制。其他人则对此进行了更广泛的撰写。此外,您可以使用像Cloudinary这样的服务为您执行此操作。

PS

PS

YMMV, but you should add this to your unicorn config as well( https://devcenter.heroku.com/articles/rails-unicorn)

YMMV,但你也应该将它添加到你的unicorn配置中(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