2011-09-29 71 views
1

要生成离线报告,我的Rails应用程序需要从Google Charts API下载图表。http.get随机地说“getaddrinfo:名称或服务未知”

问题:它正常工作的大部分时间,但有时(随机)失败,并说getaddrinfo: Name or service not known

当我得到的错误,我只是重新启动发电,通常它是成功的。
这是平时?有没有最好的做法来防止这种情况发生?
也许是重入算法,或者是更高层次的方法?

当前代码:

require 'net/http' 
    charts.each_with_index do |path, index| 
    Net::HTTP.start("chart.googleapis.com") do |http| 
    resp = http.get(path) 
    open("tmp/charts/chart" + index.to_s + ".png" ,"wb") do |file| 
     file.write(resp.body) 
    end 
    end 
end 

回答

4

...看来,你超载你的DNS机制。试试这个:

require 'net/http' 
    Net::HTTP.start("chart.googleapis.com") do |http| 
    charts.each_with_index do |path, index| 
     resp = http.get(path) 
     open("tmp/charts/chart" + index.to_s + ".png" ,"wb") do |file| 
     file.write(resp.body) 
     end 
    end 
    end 
end 
+0

这样可以避免错误,而且速度更快!谢谢! –

相关问题