Skip to content

Commit

Permalink
Add a catch for Aws::S3::Errors::NoSuchKey error
Browse files Browse the repository at this point in the history
Checks if the file has already moved before throwing an error
  • Loading branch information
carolyncole committed Jan 14, 2025
1 parent 38efd91 commit 71996b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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: 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

0 comments on commit 71996b4

Please sign in to comment.