1

在部署到 Heroku 的 Rails 3.1 项目中,我有一个 rake 任务,它根据数据库中的记录生成几十万个小 PNG,并使用将结果上传到 S3 ,一次一个文件。(在我使用 heroku 之前,该任务将文件保存在本地,一切正常。)当我使用 运行任务时heroku run:detached,它运行了一段时间,并上传了一堆文件,但最终以Connection reset by peer (Errno::ECONNRESET). 奇怪的是,我修改了脚本以捕获并重试该异常,如下所示:

file_exists = false
retry_count = 10
begin
  file_exists = directory.files.head(filekey)
rescue Errno::ECONNRESET => e
  if retry_count > 0 then
    logger.warning("Connection reset: #{e.inspect}")
    retry_count -= 1
    sleep(1)
    retry
  else
    raise
  end
end

根据堆栈跟踪,file_exists = directory.files.head(filekey)是我的代码行导致异常,所以我觉得我错过了一些关于 Ruby 异常处理的东西。我很想知道我做错了什么,因为我不能 100% 确定这是否是我的开始/救援块的问题,雾处理网络错误的方式,或者什么。

这是完整的堆栈跟踪:

Nov 07 12:08:01 ... app/run.1:  Connection reset by peer (Errno::ECONNRESET)
Nov 07 12:08:01 ... app/run.1:  /app/vendor/ruby-1.8.7/lib/ruby/1.8/openssl/buffering.rb:36:in `sysread'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/ruby-1.8.7/lib/ruby/1.8/openssl/buffering.rb:36:in `fill_rbuff'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/ruby-1.8.7/lib/ruby/1.8/openssl/buffering.rb:68:in `read'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/socket.rb:121:in `read'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/ruby-1.8.7/lib/ruby/1.8/timeout.rb:67:in `timeout'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/socket.rb:120:in `read'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/ssl_socket.rb:79:in `read'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/response.rb:21:in `parse'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/connection.rb:275:in `request_kernel'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/excon-0.16.7/lib/excon/connection.rb:103:in `request'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/fog-1.6.0/lib/fog/core/connection.rb:20:in `request'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/fog-1.6.0/lib/fog/aws/storage.rb:392:in `request'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/fog-1.6.0/lib/fog/aws/requests/storage/head_object.rb:53:in `head_object'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/fog-1.6.0/lib/fog/aws/models/storage/files.rb:98:in `head'
Nov 07 12:08:01 ... app/run.1:  /app/lib/tasks/assets.rake:45
Nov 07 12:08:01 ... app/run.1:  /app/lib/tasks/assets.rake:37:in `each'
Nov 07 12:08:01 ... app/run.1:  /app/lib/tasks/assets.rake:37
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/activerecord-3.1.8/lib/active_record/relation/batches.rb:21:in `find_each'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/activerecord-3.1.8/lib/active_record/relation/batches.rb:21:in `each'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/activerecord-3.1.8/lib/active_record/relation/batches.rb:21:in `find_each'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/activerecord-3.1.8/lib/active_record/relation/batches.rb:69:in `find_in_batches'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/activerecord-3.1.8/lib/active_record/relation/batches.rb:20:in `find_each'
Nov 07 12:08:01 ... app/run.1:  /app/lib/tasks/assets.rake:35
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `execute'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/ruby-1.8.7/lib/ruby/1.8/monitor.rb:242:in `synchronize'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `top_level'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `run'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/gems/rake-0.9.2.2/bin/rake:33
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/bin/rake:23:in `load'
Nov 07 12:08:01 ... app/run.1:  /app/vendor/bundle/ruby/1.8/bin/rake:23
Nov 07 12:08:01 ... app/run.1:  Tasks: TOP => ...:make_outlines
Nov 07 12:08:02 ... heroku/run.1:  Process exited with status 1
Nov 07 12:08:02 ... heroku/run.1:  State changed from up to complete
4

1 回答 1

1

也许试试这样:

begin
  ...
rescue Errno::ECONNRESET => e
  ...
rescue => e
  ...
end
于 2012-11-07T21:04:37.247 回答