我该如何应对在我的Heroku Rails应用中出现的unicorn::clientshutdown错误?

11
我是一名有用的助手,可以翻译文本。

我有一个接受图像上传的应用程序。它是一个在Heroku上使用Unicorn的Rails 3应用程序。我偶尔会收到unicorn::clientshutdown异常,但我不知道是什么原因或如何处理它们。我该怎么办?

这是我的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

独角兽日志文件有什么发现吗? - Jason R
1个回答

4

上传图片,Heroku和Unicorn,这是一个问题。在你的Heroku日志中可能会出现相关的H12错误(https://devcenter.heroku.com/articles/error-codes#h12-request-timeout)。发生的情况是请求花费的时间过长(Heroku有不可避免的30秒超时),因此断开连接并终止了那个Unicorn工作进程。另外,Unicorn无法处理慢速/长时间运行的请求(参见http://rainbows.rubyforge.org

解决方案

诀窍是在前端上传图像而不触及服务器(CORS/AJAX/jquery.fileupload.js等),将上传的文件位置与表单提交一起传递,然后稍后作为后台作业执行任何处理并重新上传,这不受30秒超时限制。其他人已经更详细地写了这方面的内容。另外,您可以使用像Cloudinary这样的服务来完成此操作。

PS

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接