Fix timeout bug.

This commit is contained in:
BoHung Chiu 2022-09-02 09:49:21 +08:00
parent 7ad9a1a5bd
commit 3ac3d4c7ef
1 changed files with 59 additions and 17 deletions

View File

@ -8,6 +8,32 @@ class RulingWeatherSetting
field :dataid, type: String, default: ""
field :observatory_name, type: String, default: ""
has_one :ruling_weather_cache
def net_http_get_response(uri,headers={})
port = uri.port
host = uri.host
http = Net::HTTP.new(host, port)
scheme = uri.scheme
if scheme == "https" || port.to_i == 443
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
http.open_timeout = 3
http.ssl_timeout = 1
end
http.read_timeout = 1
relative_path = (uri.path.blank? ? "/" : uri.path)
# if !relative_path.start_with?("http") && !relative_path.start_with?("/")
# relative_path = "/" + relative_path
# end
# relative_path = relative_path.sub(/^\/\.\.\/(\.\.\/)*/,'/')
# while relative_path.include?("/../") do
# relative_path = relative_path.gsub(/\/[^\/]+\/\.\.\//,'/')
# end
# relative_path = relative_path.gsub('//','/')
relative_path = URI.decode(relative_path)
relative_path += (uri.query.blank? ? '' : "?#{URI.decode(uri.query)}")
res = http.get(relative_path.gsub(" ","%20"))
res
end
def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil)
time_now = DateTime.now.utc.new_offset(self.time_offset)
today = time_now.strftime("%Y-%m-%d")
@ -22,15 +48,21 @@ class RulingWeatherSetting
"timeTo" => endt,
"limit" => 1,
"locationName" => custom_location}
res = Net::HTTP.get_response(URI.parse("#{url}?#{data.to_query}"))
content = JSON.parse(res.body)
cahche_model = self.ruling_weather_cache
if cahche_model.nil?
cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self)
timeout_flag = false
begin
cahche_model = self.ruling_weather_cache
if cahche_model.nil?
cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self)
end
cache = cahche_model.cache rescue {}
res = net_http_get_response(URI.parse("#{url}?#{data.to_query}"))
content = JSON.parse(res.body)
weather_data = get_weather_data(content) rescue {}
rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
timeout_flag = true
weather_data = {}
end
cache = cahche_model.cache rescue {}
weather_data = get_weather_data(content) rescue {}
if (weather_data.count == 0 rescue true)
if timeout_flag || (weather_data.count == 0 rescue true)
mint = cache["mint"].to_i
maxt = cache["maxt"].to_i
avgt = cache["avgt"].to_i
@ -58,9 +90,10 @@ class RulingWeatherSetting
else
wx_code = wx_code.to_s
end
svg_type = "day"
if time_now.hour >= 18
svg_type = "night"
else
svg_type = "day"
end
wx_svg = "/assets/ruling_weather/svg_icon/#{svg_type}/#{wx_code}.svg"
custom_observatory_name = custom_observatory_name || self.observatory_name
@ -69,14 +102,22 @@ class RulingWeatherSetting
"locationName" => custom_observatory_name}
rain = nil
if custom_observatory_name.present?
url2 = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0002-001.json"
res2 = Net::HTTP.get_response(URI.parse("#{url2}?#{data2.to_query}"))
content2 = JSON.parse(res2.body)
weather_data2 = get_weather_data(content2) rescue {}
if (weather_data2.count == 0 rescue true)
rain = cache["rain"]
if timeout_flag
rain = cache["rain"].to_f rescue 0.0
else
rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0
url2 = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0002-001.json"
begin
res2 = net_http_get_response(URI.parse("#{url2}?#{data2.to_query}"))
content2 = JSON.parse(res2.body)
weather_data2 = get_weather_data(content2) rescue {}
rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
weather_data2 = {}
end
if (weather_data2.count == 0 rescue true)
rain = cache["rain"].to_f rescue 0.0
else
rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0
end
end
end
result = {"mint" => mint,
@ -86,6 +127,7 @@ class RulingWeatherSetting
"uvi" => uvi,
"uvi_text" => uvi_text,
"rh" => rh,
"wx_code" => wx_code,
"wx_text" => wx_text,
"wx_svg" => wx_svg,
"ws" => ws,
@ -110,7 +152,7 @@ class RulingWeatherSetting
"timeTo" => endt,
"limit" => 1,
"locationName" => custom_location}
res = Net::HTTP.get_response(URI.parse("#{url}?#{data.to_query}"))
res = net_http_get_response(URI.parse("#{url}?#{data.to_query}"))
content = JSON.parse(res.body)
weather_data = get_weather_data(content) rescue {}
end