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 :dataid, type: String, default: ""
field :observatory_name, type: String, default: "" field :observatory_name, type: String, default: ""
has_one :ruling_weather_cache 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) def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil)
time_now = DateTime.now.utc.new_offset(self.time_offset) time_now = DateTime.now.utc.new_offset(self.time_offset)
today = time_now.strftime("%Y-%m-%d") today = time_now.strftime("%Y-%m-%d")
@ -22,15 +48,21 @@ class RulingWeatherSetting
"timeTo" => endt, "timeTo" => endt,
"limit" => 1, "limit" => 1,
"locationName" => custom_location} "locationName" => custom_location}
res = Net::HTTP.get_response(URI.parse("#{url}?#{data.to_query}")) timeout_flag = false
content = JSON.parse(res.body) begin
cahche_model = self.ruling_weather_cache cahche_model = self.ruling_weather_cache
if cahche_model.nil? if cahche_model.nil?
cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self) cahche_model = RulingWeatherCache.create(:cache=>{},:ruling_weather_setting=>self)
end end
cache = cahche_model.cache rescue {} 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 {} weather_data = get_weather_data(content) rescue {}
if (weather_data.count == 0 rescue true) rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
timeout_flag = true
weather_data = {}
end
if timeout_flag || (weather_data.count == 0 rescue true)
mint = cache["mint"].to_i mint = cache["mint"].to_i
maxt = cache["maxt"].to_i maxt = cache["maxt"].to_i
avgt = cache["avgt"].to_i avgt = cache["avgt"].to_i
@ -58,9 +90,10 @@ class RulingWeatherSetting
else else
wx_code = wx_code.to_s wx_code = wx_code.to_s
end end
svg_type = "day"
if time_now.hour >= 18 if time_now.hour >= 18
svg_type = "night" svg_type = "night"
else
svg_type = "day"
end end
wx_svg = "/assets/ruling_weather/svg_icon/#{svg_type}/#{wx_code}.svg" wx_svg = "/assets/ruling_weather/svg_icon/#{svg_type}/#{wx_code}.svg"
custom_observatory_name = custom_observatory_name || self.observatory_name custom_observatory_name = custom_observatory_name || self.observatory_name
@ -69,16 +102,24 @@ class RulingWeatherSetting
"locationName" => custom_observatory_name} "locationName" => custom_observatory_name}
rain = nil rain = nil
if custom_observatory_name.present? if custom_observatory_name.present?
if timeout_flag
rain = cache["rain"].to_f rescue 0.0
else
url2 = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0002-001.json" 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}")) begin
res2 = net_http_get_response(URI.parse("#{url2}?#{data2.to_query}"))
content2 = JSON.parse(res2.body) content2 = JSON.parse(res2.body)
weather_data2 = get_weather_data(content2) rescue {} weather_data2 = get_weather_data(content2) rescue {}
rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
weather_data2 = {}
end
if (weather_data2.count == 0 rescue true) if (weather_data2.count == 0 rescue true)
rain = cache["rain"] rain = cache["rain"].to_f rescue 0.0
else else
rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0 rain = weather_data2["NOW"]["elementValue"].to_f rescue 0.0
end end
end end
end
result = {"mint" => mint, result = {"mint" => mint,
"maxt" => maxt, "maxt" => maxt,
"avgt" => avgt, "avgt" => avgt,
@ -86,6 +127,7 @@ class RulingWeatherSetting
"uvi" => uvi, "uvi" => uvi,
"uvi_text" => uvi_text, "uvi_text" => uvi_text,
"rh" => rh, "rh" => rh,
"wx_code" => wx_code,
"wx_text" => wx_text, "wx_text" => wx_text,
"wx_svg" => wx_svg, "wx_svg" => wx_svg,
"ws" => ws, "ws" => ws,
@ -110,7 +152,7 @@ class RulingWeatherSetting
"timeTo" => endt, "timeTo" => endt,
"limit" => 1, "limit" => 1,
"locationName" => custom_location} "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) content = JSON.parse(res.body)
weather_data = get_weather_data(content) rescue {} weather_data = get_weather_data(content) rescue {}
end end