Commit 1e8b748f authored by DJ Mountney's avatar DJ Mountney
Browse files

Merge branch 'feat/add_retry_to_net_fetcher' into '7.0.10-stable'

Feature: add retry to net fetcher

See merge request gitlab-org/omnibus!21
parents c80716bb 0b0b6cc4
......@@ -80,20 +80,24 @@ module Omnibus
# @return [void]
#
def fetch
log.info(log_key) { "Downloading from `#{download_url}'" }
create_required_directories
debug_data = download
verify_checksum!
rescue ChecksumMismatch => original_error
debug_msg = <<-MSG_TEXT
retry_with_limit(exception: ChecksumMismatch) do
begin
log.info(log_key) { "Downloading from `#{download_url}'" }
create_required_directories
debug_data = download
verify_checksum!
rescue ChecksumMismatch => original_error
debug_msg = <<-MSG_TEXT
Detected checksum mismatch.
DEBUGGING INFO FROM DOWNLOAD
#{debug_data}
MSG_TEXT
MSG_TEXT
log.info(log_key) { debug_msg }
raise original_error
log.info(log_key) { debug_msg }
raise original_error
end
end
end
#
......@@ -334,5 +338,23 @@ module Omnibus
def tar
Omnibus.which("gtar") ? "gtar" : "tar"
end
#
# Helper method to retry something with count limit.
#
def retry_with_limit(exception: Exception, retries: 2)
try_count = 0
begin
yield
rescue exception => e
try_count += 1
if try_count <= retries
log.info(log_key) { "Retry(#{try_count})" }
retry
else
raise e
end
end
end
end
end
......@@ -253,6 +253,21 @@ module Omnibus
expect(retry_count).to eq(Omnibus::Config.fetcher_retries)
end
context "when network unstable" do
context "when download failed and the checksum is invalid" do
let(:source_md5) { "invalid_md5_checksum" }
it "raises an exception" do
output = capture_logging do
expect { fetch! }.to raise_error(ChecksumMismatch)
end
expect(output).to include("Retry(1)")
expect(output).to include("Retry(2)")
expect(output).not_to include("Retry(3)")
end
end
end
context "when the file is less than 10240 bytes" do
let(:source_url) { "https://downloads.chef.io/packages-chef-io-public.key" }
let(:source_md5) { "369efc3a19b9118cdf51c7e87a34f266" }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment