Skip to content

Commit

Permalink
Merge pull request #62 from cul/Google-Cloud-UnavailableError-retry
Browse files Browse the repository at this point in the history
Atc::Gcp::StorageUploader retries an upload when a Google::Cloud::UnavailableError is encountered
  • Loading branch information
elohanlon authored Dec 20, 2024
2 parents 3b703b7 + 106ab84 commit 51c4f79
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Metrics/MethodLength:
- lib/atc/utils/aws_multipart_checksum_utils.rb
- lib/atc/aws/s3_uploader.rb
- lib/atc/utils/aws_checksum_utils.rb
- lib/atc/gcp/storage_uploader.rb

Rails/Output:
Exclude:
Expand Down
14 changes: 9 additions & 5 deletions lib/atc/gcp/storage_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def upload_file(local_file_path, object_key, **options)
calculate_crc32c(local_file_path, verbose: options[:verbose])

puts 'Performing upload...' if options[:verbose]
bucket.create_file(
local_file_path, object_key,
content_type: BestType.mime_type.for_file_name(local_file_path),
crc32c: precalculated_whole_file_crc32c, metadata: options[:metadata]
)

Retriable.retriable(on: [Google::Cloud::UnavailableError], tries: 3, base_interval: 1) do
bucket.create_file(
local_file_path, object_key,
content_type: BestType.mime_type.for_file_name(local_file_path),
crc32c: precalculated_whole_file_crc32c, metadata: options[:metadata]
)
end

true
rescue Google::Cloud::InvalidArgumentError, Google::Apis::ClientError => e
wrap_and_re_raise_gcp_storage_client_error(e, local_file_path, object_key)
Expand Down
13 changes: 13 additions & 0 deletions spec/atc/gcp/storage_uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,18 @@
end
end
end

context 'retry behavior' do
it 'retries when it encounters a Google::Cloud::UnavailableError, and re-raises after retries are exhausted' do
Tempfile.create(['example-file-to-checksum', '.tiff']) do |f|
f.write('A')
f.flush
expect(bucket).to receive(:create_file).exactly(3).times.and_raise(Google::Cloud::UnavailableError)
expect {
storage_uploader.upload_file(f.path, object_key, metadata: { 'metadata-key' => 'metadata-value' })
}.to raise_error(Google::Cloud::UnavailableError)
end
end
end
end
end

0 comments on commit 51c4f79

Please sign in to comment.