ruby-on-rails - Errno::ETIMEDOUT:连接超时 - connect(2)

在生产中,我偶尔遇到以下错误:

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/

相关文章:

iphone - 选择时 Xcode UITableView 行未突出显示

opengl - 可以只在 opengl 中移动相机而不重绘场景吗?

sql - 使用 ROWNUM 在 Oracle 中获取质数记录

jquery - 如何使用 jQuery 取消按钮的提交

macos - 如何在终端加载 I/O 套件驱动程序扩展?

erlang - 在没有列表到字符串翻译的情况下格式化 Erlang 术语

asp.net - Web API 可以将XML 转换成Json 吗?

matlab - 将向量转置到三维

php - 获取最近过去的星期六(上星期六)

image - 如何使用 htaccess 更改图像路径?