From 1ef665007ab3a5baa1173738a297bdde9436e3aa Mon Sep 17 00:00:00 2001 From: bohung Date: Fri, 2 Sep 2022 10:16:22 +0800 Subject: [PATCH] Fix timeout bug. --- app/helpers/ruling_buses_helper.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/helpers/ruling_buses_helper.rb b/app/helpers/ruling_buses_helper.rb index 047779d..2ac50cf 100644 --- a/app/helpers/ruling_buses_helper.rb +++ b/app/helpers/ruling_buses_helper.rb @@ -24,6 +24,8 @@ module RulingBusesHelper include HTTParty base_uri 'https://ptx.transportdata.tw/MOTC/v2' + open_timeout 3 + read_timeout 1 format :json headers( @@ -125,7 +127,11 @@ module RulingBusesHelper '(' + v.map{|r| "RouteName/#{bus_info.locale} eq '#{r}'"}.join(' or ') + '))') end.join(' or ') url = "/Bus/EstimatedTimeOfArrival/City/#{city}?$select=Direction,RouteName,NextBusTime&$filter=#{filters}&$format=JSON&$orderby=RouteName/#{locale}" - bus_data = JSON.parse(self.get(url).body) + begin + bus_data = JSON.parse(self.get(url).body) + rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout + return nil + end if bus_data.class==Hash return nil end @@ -135,7 +141,11 @@ module RulingBusesHelper '(' + v.map{|r| "RouteName/#{bus_info.locale} eq '#{r}'"}.join(' or ') + '))') end.join(' or ') url2 = "/Bus/EstimatedTimeOfArrival/InterCity?$select=Direction,RouteName,NextBusTime&$filter=#{filters}&$format=JSON&$orderby=RouteName/#{locale}" - pbus_data = JSON.parse(self.get(url2).body) + begin + pbus_data = JSON.parse(self.get(url2).body) + rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout + pbus_data = [] + end else pbus_data = [] end