我在Heroku托管的Rails应用程序中进行了一项操作,该应用程序在后台进程中运行,并且呼叫一个速度缓慢的政府网站。我收到了一个我无法解决的超时错误(在下面的底部)。
读取超时出现在请求的20秒内。
以下行导致问题:
Mechanize.new.get('http://ai.fmcsa.dot.gov/SMS/Data/Search.aspx')
是否有修复此问题的环境变量?我还缺少其他什么东西?
Errno::ETIMEDOUT: Connection timed out - connect(2)
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `initialize'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `open'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `block in connect'
from /usr/local/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /usr/local/lib/ruby/1.9.1/timeout.rb:89:in `timeout'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent/ssl_reuse.rb:29:in `connect'
from /usr/local/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /usr/local/lib/ruby/1.9.1/net/http.rb:632:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:768:in `reset'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:503:in `connection_for'
from /app/vendor/bundle/ruby/1.9.1/gems/net-http-persistent-2.7/lib/net/http/persistent.rb:806:in `request'
from /app/vendor/bundle/ruby/1.9.1/gems/mechanize-2.5.1/lib/mechanize/http/agent.rb:258:in `fetch'
from /app/vendor/bundle/ruby/1.9.1/gems/mechanize-2.5.1/lib/mechanize.rb:407:in `get'
from /app/app/models/inspection.rb:14:in `fmcsa_summary'
from (irb):5
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
答案 0 :(得分:1)
作为临时修复的解决方案是通过Mechanize使用代理服务器。我要连接的服务器和Heroku EC2实例的IP地址范围之间必定存在一些问题。
agent = Mechanize.new { |a| a.set_proxy(url,port,username,password) }
如果有人知道原始问题的根本原因,那仍然会有所帮助。