当前位置:  开发笔记 > 后端 > 正文

通过ruby OpenURI下载文件时间歇性EOFError

如何解决《通过rubyOpenURI下载文件时间歇性EOFError》经验,为你挑选了0个好方法。

我有一个设置,其中cron作业定期从外部服务(Twilio)下载mp3文件,然后使用Paperclip将文件上传到Amazon S3.Resque在后台处理该过程.

以下是处理从Twilio下载以及随后附加到Paperclip的代码:

# Perform transfer from Twilio to S3
def self.perform(group_recording_id = nil)
  gr = GroupRecording.find(group_recording_id)
  # ...
  recording = TwilioClient.account.recordings.get(gr.external_id)

  if recording.present?
    # ....
    gr.audio_file = download_remote_file(gr.twilio_mp3_url)
    gr.save
  end
end

def download_remote_file(url)
  io = open(URI.parse(url))  

  # overrides Paperclip::Upfile#original_filename
  def io.original_filename
    base_uri.path.split('/').last
  end
  io.original_filename.blank? ? nil : io
end

这失败了EOFError:

EOFError
end of file reached
/usr/local/lib/ruby/1.9.1/openssl/buffering.rb:145:in `sysread_nonblock'
/usr/local/lib/ruby/1.9.1/openssl/buffering.rb:145:in `read_nonblock'
/usr/local/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill'
/usr/local/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil'
/usr/local/lib/ruby/1.9.1/net/protocol.rb:126:in `readline'
/usr/local/lib/ruby/1.9.1/net/http.rb:2219:in `read_status_line'
/usr/local/lib/ruby/1.9.1/net/http.rb:2208:in `read_new'
/usr/local/lib/ruby/1.9.1/net/http.rb:1191:in `transport_request'
/usr/local/lib/ruby/1.9.1/net/http.rb:1177:in `request'
/app/vendor/bundle/ruby/1.9.1/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
/usr/local/lib/ruby/1.9.1/open-uri.rb:312:in `block in open_http'
/usr/local/lib/ruby/1.9.1/net/http.rb:627:in `start'
/usr/local/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
/usr/local/lib/ruby/1.9.1/open-uri.rb:769:in `buffer_open'
/usr/local/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
/usr/local/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
/usr/local/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
/usr/local/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
/usr/local/lib/ruby/1.9.1/open-uri.rb:671:in `open'
/usr/local/lib/ruby/1.9.1/open-uri.rb:33:in `open'
/app/app/models/group_recording.rb:112:in `download_remote_file'
/app/app/models/group_recording.rb:85:in `perform'

以上是最常见的错误,但我也遇到过这个问题:

Errno::ECONNRESET
Connection reset by peer
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/openssl/buffering.rb:145:in `sysread_nonblock'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/openssl/buffering.rb:145:in `read_nonblock'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/protocol.rb:86:in `read'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:2424:in `read_body_0'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:2379:in `read_body'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:321:in `block (2 levels) in open_http'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:1194:in `block in transport_request'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:2342:in `reading_body'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:1193:in `transport_request'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:1177:in `request'
/app/vendor/bundle/ruby/1.9.1/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:312:in `block in open_http'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/net/http.rb:627:in `start'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:769:in `buffer_open'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:671:in `open'
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/open-uri.rb:33:in `open'
/app/app/models/group_recording.rb:113:in `download_remote_file'
/app/app/models/group_recording.rb:86:in `perform'

问题是断断续续的,最近刚刚在heroku的生产环境中开始发生,相关代码没有任何变化.该应用程序目前ruby 1.9.3-p429在heroku上.问题也出现在本地,但不太常见.我尝试了相同和较低的红宝石版本(低至1.9.3-p194).

有没有人遇到过类似的问题?在线搜索错误已经发现了类似的错误,但是在完全不同的情况下.

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有