Skip to content

Commit 6ceafd9

Browse files
committed
add retry logic to online distro info methods
1 parent 0d03829 commit 6ceafd9

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

internal/lib/distro_info.rb

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,22 @@ def valid_distro_name?(name)
6868
def fetch_latest_nginx_version_from_launchpad_api(distro)
6969
['Updates', 'Security', 'Release'].each do |pocket|
7070
url = "https://api.launchpad.net/1.0/ubuntu/+archive/primary?ws.op=getPublishedBinaries&binary_name=nginx&exact_match=true&distro_arch_series=https://api.launchpad.net/1.0/ubuntu/#{distro}/amd64&status=Published&pocket=#{pocket}"
71-
if RUBY_VERSION >= '2.5'
72-
data = URI.open(url) do |io|
73-
io.read
74-
end
75-
else
76-
data = open(url) do |io|
77-
io.read
78-
end
79-
end
71+
retries = 0
72+
data = begin
73+
if RUBY_VERSION >= '2.5'
74+
URI.open(url, &:read)
75+
else
76+
open(url, &:read)
77+
end
78+
rescue
79+
if (retries += 1) <= 3
80+
sleep(retries)
81+
retry
82+
else
83+
raise
84+
end
85+
end
86+
8087
entry = JSON.parse(data)['entries'][0]
8188
if entry
8289
return entry['binary_package_version']
@@ -92,15 +99,26 @@ def extract_nginx_version(os, distro, sanitize)
9299
version = fetch_latest_nginx_version_from_launchpad_api(distro)
93100
elsif DEBIAN_DISTRIBUTIONS.key?(distro)
94101
url = "https://packages.debian.org/search?suite=#{distro}&exact=1&searchon=names&keywords=nginx"
95-
if RUBY_VERSION >= '2.5'
96-
doc = URI.open(url) do |io|
97-
Nokogiri.XML(io)
98-
end
99-
else
100-
doc = open(url) do |io|
101-
Nokogiri.XML(io)
102-
end
103-
end
102+
retries = 0
103+
doc = begin
104+
if RUBY_VERSION >= '2.5'
105+
URI.open(url) do |io|
106+
Nokogiri.XML(io)
107+
end
108+
else
109+
open(url) do |io|
110+
Nokogiri.XML(io)
111+
end
112+
end
113+
rescue
114+
if (retries += 1) <= 3
115+
sleep(retries)
116+
retry
117+
else
118+
raise
119+
end
120+
end
121+
104122
version = doc.at_css('#psearchres ul li').text.lines.select{|s|s.include? ": all" or s.include? ": amd64 arm64"}.first.strip.split.first.chomp(':')
105123
end
106124
File.write(cache_file,version)

0 commit comments

Comments
 (0)