diff --git a/app/jobs/approved_file_move_job.rb b/app/jobs/approved_file_move_job.rb index 61f89f9f7..a40831607 100644 --- a/app/jobs/approved_file_move_job.rb +++ b/app/jobs/approved_file_move_job.rb @@ -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}" diff --git a/spec/jobs/approved_file_copy_job_spec.rb b/spec/jobs/approved_file_copy_job_spec.rb index 5be2c8f51..5e192a8d2 100644 --- a/spec/jobs/approved_file_copy_job_spec.rb +++ b/spec/jobs/approved_file_copy_job_spec.rb @@ -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(