Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a catch for Aws::S3::Errors::NoSuchKey error #2011

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/jobs/approved_file_move_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ def perform(work_id:, source_bucket:, source_key:, target_bucket:, target_key:,
@source_bucket = source_bucket
@source_key = source_key

resp = service.copy_file(source_key: key, target_bucket:, target_key:, size:)

unless resp.successful?
raise "Error copying #{key} to #{target_bucket}/#{target_key} Response #{resp.to_json}"
begin
resp = service.copy_file(source_key: key, target_bucket:, target_key:, size:)
unless resp.successful?
raise "Error copying #{key} to #{target_bucket}/#{target_key} Response #{resp.to_json}"
end
rescue Aws::S3::Errors::NoSuchKey => error
status = service.check_file(bucket: source_bucket, key:)
unless status
raise "Missing source file #{key} can not copy to #{target_bucket}/#{target_key} Error: #{error}"
end
end

status = service.check_file(bucket: target_bucket, key: target_key)
unless status
raise "File check was not valid #{source_key} to #{target_bucket}/#{target_key} Response #{status.to_json}"
Expand Down
17 changes: 17 additions & 0 deletions spec/jobs/approved_file_copy_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@
end
end

context "the original key is missing" do
before do
allow(fake_s3_service).to receive(:copy_file).and_raise(Aws::S3::Errors::NoSuchKey.new(nil, nil))
allow(fake_s3_service).to receive(:check_file).and_return(false)
end
it "runs an aws copy, but no delete" do
expect do
perform_enqueued_jobs do
job
end
end .to raise_error(/Missing source file \/example-bucket\/10.34770\/ackh-7y71\/#{work.id}\/test_key can not copy/)
expect(fake_s3_service).to have_received(:copy_file).with(size: 200, source_key: "/example-bucket/#{s3_file.key}",
target_bucket: "example-bucket-post", target_key: s3_file.key)
expect(fake_s3_service).not_to have_received(:delete_s3_object).with("example-bucket/#{s3_file.key}")
expect(fake_s3_service).not_to have_received(:delete_s3_object).with(work.s3_object_key, bucket: "example-bucket")
end
end
context "when the ApprovedUploadSnapshot cannot be found" do
subject(:output) do
described_class.perform_now(
Expand Down