Skip to content

Commit 3ab3d76

Browse files
authored
Merge pull request #2688 from ojeytonwilliams/fix/retry-failed-downloads
fix: retry failed downloads
2 parents d999b89 + fb2fb06 commit 3ab3d76

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

lib/tasks/docs.thor

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ class DocsCLI < Thor
235235
puts 'Docs -- BEGIN'
236236

237237
require 'open-uri'
238+
require 'net/http'
238239
require 'thread'
239240

240241
docs = Docs.all_versions
@@ -250,15 +251,30 @@ class DocsCLI < Thor
250251
['index.json', 'meta.json'].each do |filename|
251252
json = "https://documents.devdocs.io/#{doc.path}/#{filename}?#{time}"
252253
begin
253-
URI.open(json, "Accept-Encoding" => "identity") do |file|
254-
mutex.synchronize do
255-
path = File.join(dir, filename)
256-
File.write(path, file.read)
254+
attempts = 0
255+
256+
begin
257+
attempts += 1
258+
259+
URI.open(json, "Accept-Encoding" => "identity") do |file|
260+
mutex.synchronize do
261+
path = File.join(dir, filename)
262+
File.write(path, file.read)
263+
end
257264
end
265+
rescue Net::OpenTimeout, Net::ReadTimeout => e
266+
if attempts <= 3
267+
wait_seconds = 2**(attempts - 1)
268+
puts "Docs -- Retrying #{json} in #{wait_seconds}s (#{e.class}: #{e.message})"
269+
sleep(wait_seconds)
270+
retry
271+
end
272+
273+
raise
258274
end
259275
rescue => e
260-
puts "Docs -- Failed to download #{json}!"
261-
throw e
276+
puts "Docs -- Failed to download #{json} after #{attempts} attempts!"
277+
raise
262278
end
263279
end
264280

0 commit comments

Comments
 (0)