在生产中,我偶尔遇到以下错误:
Errno::ETIMEDOUT: Connection timed out - connect(2)
只有当我使用包含由 paperclip/aws-sdk 上传到 s3 的图像的 prawn gem 生成 PDF 时,它似乎才会发生。对于每天使用数百次(没有问题)的操作,这可能每周只发生几次,但当它失败时会导致 500 错误。
痕迹是:
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`initialize'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`connect'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/timeout.rb:53:in
`timeout'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/timeout.rb:101:in
`timeout'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:560:in
`connect'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:553:in
`do_start'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/net/http.rb:542:in
`start'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:242:in
`open_http'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:616:in
`buffer_open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:164:in
`open_loop'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:162:in
`catch'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:162:in
`open_loop'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:132:in
`open_uri'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:518:in
`open'
/opt/ruby-enterprise-1.8.7-2011.03/lib/ruby/1.8/open-uri.rb:30:in
`open'
....rb:57:in `render_image_to_pdf'
第 57 行:
pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
设置:
Rails 3.0.10
Ruby 1.8.7EE
Prawn 0.11.1
AWS-SDK 1.3.3
Paperclip 2.5.2
我该怎么做才能避免这个错误?
最佳答案
一种选择是捕获异常并重试:
begin
pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
rescue Errno::ETIMEDOUT
# try one more time, or use retry with a counter to attempt a limited number of times
pdf.image open(image.expiring_url(30.minutes, :full)), :width => 300, :position => 20
end
您可能还想进行一些诊断(记录或生成空气制动器)来通知您,以便您可以查看故障是否存在某种模式。
https://stackoverflow.com/questions/9369435/