feat(ArchiveUploader.upload_with_chunking_wrapper): retry once on error

This commit is contained in:
Martin 2022-04-05 15:05:22 +02:00 committed by mfo
parent c72ba43c55
commit 9e8807d12a
2 changed files with 15 additions and 3 deletions

View file

@ -48,11 +48,11 @@ class ArchiveUploader
params = blob_default_params(filepath).merge(byte_size: File.size(filepath), params = blob_default_params(filepath).merge(byte_size: File.size(filepath),
checksum: Digest::SHA256.file(filepath).hexdigest) checksum: Digest::SHA256.file(filepath).hexdigest)
blob = ActiveStorage::Blob.create_before_direct_upload!(**params) blob = ActiveStorage::Blob.create_before_direct_upload!(**params)
if syscall_to_custom_uploader(blob) if retryable_syscall_to_custom_uploader(blob)
return blob return blob
else else
blob.purge blob.purge
fail "custom archive attachment failed, should it be retried ?" fail "custom archive attachment failed twice, retry later"
end end
end end
@ -73,6 +73,18 @@ class ArchiveUploader
@namespaced_object_key ||= "archives/#{Date.today.strftime("%Y-%m-%d")}/#{SecureRandom.uuid}" @namespaced_object_key ||= "archives/#{Date.today.strftime("%Y-%m-%d")}/#{SecureRandom.uuid}"
end end
def retryable_syscall_to_custom_uploader(blob)
limit_to_retry = 1
begin
syscall_to_custom_uploader(blob)
rescue
if limit_to_retry > 0
limit_to_retry = limit_to_retry - 1
retry
end
end
end
def syscall_to_custom_uploader(blob) def syscall_to_custom_uploader(blob)
system(ENV.fetch('ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH').to_s, filepath, blob.key, exception: true) system(ENV.fetch('ACTIVE_STORAGE_BIG_FILE_UPLOADER_WITH_ENCRYPTION_PATH').to_s, filepath, blob.key, exception: true)
end end

View file

@ -82,7 +82,7 @@ describe ProcedureArchiveService do
it 'does not retry more than once' do it 'does not retry more than once' do
expect(uploader).to receive(:syscall_to_custom_uploader).with(anything).twice.and_raise(StandardError, "BOOM") expect(uploader).to receive(:syscall_to_custom_uploader).with(anything).twice.and_raise(StandardError, "BOOM")
expect { uploader.send(:upload_with_chunking_wrapper) } expect { uploader.send(:upload_with_chunking_wrapper) }
.to raise_error(StandardError, "BOOM") .to raise_error(RuntimeError, "custom archive attachment failed twice, retry later")
end end
end end
end end