2

我正在Ruby 1.9.3-p327中构建一个应用程序,它获取解析一些页面(抓取),然后根据一些值将一些列插入/更新到数据库中。为了获取解析,应用程序使用 Mechanize gem,并且通过 activerecord gem 访问数据库(MySQL)。

我遇到的奇怪问题是,有时会随机引发Timeout::Error异常,有时永远不会发生,但可能再过两天就会发生,并且记录或页面类型不同。异常的日志是:

/root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:146:in `rescue in rbuf_fill': too many connection resets (due to Timeout::Error - Timeout::Error) after 0 requests on 21716860, last used 1378984537.2796552 seconds ago (Net::HTTP::Persistent::Error)
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:140:in `rbuf_fill'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/1.9.1/net/http.rb:1293:in `request'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:986:in `request'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:257:in `fetch'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/mechanize-2.7.2/lib/mechanize.rb:432:in `get'
    from /root/notificador-corte/lib/downloader.rb:10:in `fetch'
    from /root/notificador-corte/worker.rb:63:in `fetch_page'
    from /root/notificador-corte/worker.rb:49:in `process_causa'
    from /root/notificador-corte/worker.rb:41:in `block in worker_main_cycle'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
    from /root/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:13:in `each'
    from /root/notificador-corte/worker.rb:39:in `worker_main_cycle'
    from /root/notificador-corte/worker.rb:26:in `run'
    from /root/notificador-corte/app.rb:12:in `<main>'

downloader.rb第10 行包含方法 fetch 的定义:

def fetch(url)
    begin
      @agent.get(url) )
    rescue Errno::ETIMEDOUT, Timeout::Error => exception
    end
  end

第63 行的worker.rb包含对 fetch 方法的调用。

阅读文档,说我应该尝试为代理(机械化)设置read_timeoutopen_timeout属性,并尝试使用idle_timeoutkeep_alive,但错误仍然是随机性的。

Gemfile 的内容是:

gem 'activerecord', "~> 4.0.0" 
gem 'mechanize', "~> 2.7.1"
gem 'mysql', '~> 2.9.1'
gem 'actionmailer', "~> 4.0.0" 
gem 'rspec', "~> 2.14.1"
4

2 回答 2

1

我认为这不一定是您的代码中的错误或自行机械化它。很可能是网络问题。

我宁愿在该rescue语句中实施一项策略,以便您确保无论何时发生此错误,您都确保稍后“重试”。

于 2014-03-27T15:11:29.727 回答
0

当你救援时,你有

 Errno::ETIMEDOUT

这是拼写错误吗?还是我不熟悉的东西?

您的问题很可能是一些不良网站或链接。当我浏览整个互联网时,我遇到了各种各样的问题。我发现最好捕获所有错误,打印错误消息,然后继续下一个可能的操作。这样你的爬虫就不会停止在坏情况下,你可以在问题出现时返回并解决问题。

于 2014-07-22T22:03:52.593 回答