diff --git a/app/jobs/dspace_file_copy_job.rb b/app/jobs/dspace_file_copy_job.rb index 5cad2f784..4a562d7f0 100644 --- a/app/jobs/dspace_file_copy_job.rb +++ b/app/jobs/dspace_file_copy_job.rb @@ -32,5 +32,9 @@ def update_migration(migration_snapshot_id, s3_key, s3_size, work) migration_snapshot.mark_complete(S3File.new(filename: s3_key, last_modified: DateTime.now, size: s3_size, checksum: "", work:)) migration_snapshot.save! end + + rescue ActiveRecord::StatementInvalid + ActiveRecord::Base.connection_pool.release_connection + retry end end diff --git a/spec/jobs/dspace_file_copy_job_spec.rb b/spec/jobs/dspace_file_copy_job_spec.rb index e01ef5a10..0c6924430 100644 --- a/spec/jobs/dspace_file_copy_job_spec.rb +++ b/spec/jobs/dspace_file_copy_job_spec.rb @@ -25,6 +25,29 @@ target_bucket: "work-bucket", target_key: "abc/123/#{work.id}/test_key") end + context "when an ActiveRecord::StatementInvalid error occurs" do + before do + @error_count = 0 + allow(MigrationUploadSnapshot).to receive(:find) do + if @error_count == 0 + @error_count = 1 + raise ActiveRecord::StatementInvalid, "error" + else + migration_snapshot + end + end + end + + it "runs the copy and updates the snapshot" do + allow(migration_snapshot).to receive(:"save!").and_call_original + perform_enqueued_jobs { job } + expect(fake_s3_service).to have_received(:copy_file).with(size: 10_759, source_key: "example-bucket-dspace/10-34770/ackh-7y71/test_key", + target_bucket: "work-bucket", target_key: "abc/123/#{work.id}/test_key") + + expect(migration_snapshot).to have_received(:"save!") + end + end + context "when the files is a directory" do let(:s3_file) { FactoryBot.build :s3_file, filename: "10-34770/ackh-7y71/dir/", size: 0, filename_display: "abc/123/#{work.id}/dir/" }