2012-11-07 43 views
0

在我部署到Heroku的Rails 3.1項目中,我有一個rake任務,根據數據庫中的記錄生成幾十萬個小PNG,並使用fog將結果上傳到S3 ,一次一個文件。 (在我使用heroku之前,任務在本地保存了文件,並且一切正常。)當我使用heroku run:detached運行任務時,它運行了一段時間,並上傳了一堆文件,但最終以Connection reset by peer (Errno::ECONNRESET)失敗。有什麼奇怪的這個是我修改的腳本來捕獲和重試例外,像這樣:未能在Ruby代碼中捕獲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 
+0

您需要登錄RETRY_COUNT,以確保它永遠不會重試10次,你可能會驚訝連接了出來超過10秒 – bjhaid

+0

這並不是最終的問題,但這是事實。理想的解決方案可能會涉及更復雜的等待和重試周期。 –

回答

1

也許嘗試這樣的:

begin 
    ... 
rescue Errno::ECONNRESET => e 
    ... 
rescue => e 
    ... 
end 
+0

好主意。嘗試,會報告回來。 –

+0

嗯,當然:異常實際上是Excon :: Errors :: SocketError。 Excon必須包裝ECONNRESET。我希望Ruby能夠將堆棧跟蹤包含在異常類中,但是也許有一個原因,我不知道它呢? –

相關問題