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
這樣可以避免錯誤,而且速度更快!謝謝! –