diff --git a/app/jobs/scsb_import_full_job.rb b/app/jobs/scsb_import_full_job.rb index 75e0c852..be5641cc 100644 --- a/app/jobs/scsb_import_full_job.rb +++ b/app/jobs/scsb_import_full_job.rb @@ -18,8 +18,14 @@ def created_dump(event) end def delete_stale_files - FileUtils.rm Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.zip") - FileUtils.rm Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.xml") - FileUtils.rm Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.csv") + files_to_delete = Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.zip") + .concat(Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.xml")) + .concat(Dir.glob("#{ENV['SCSB_PARTNER_UPDATE_DIRECTORY']}/*.csv")) + files_to_delete.each do |file| + FileUtils.rm file + rescue Errno::ENOENT + Rails.logger.warn("Attempted to delete file #{file}, but it was no longer present on the filesystem") + next + end end end diff --git a/spec/jobs/scsb_import_full_job_spec.rb b/spec/jobs/scsb_import_full_job_spec.rb index 0bb541c0..5bfea981 100644 --- a/spec/jobs/scsb_import_full_job_spec.rb +++ b/spec/jobs/scsb_import_full_job_spec.rb @@ -14,7 +14,7 @@ expect(Scsb::PartnerUpdates).to have_received(:full) end - describe 'when there stale files in the update directory path' do + describe 'when there are stale files in the update directory path' do let(:update_directory_path) { Rails.root.join("tmp", "specs", "update_directory") } before do @@ -36,5 +36,20 @@ expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'scsb_update_20240108_183400_1.xml'))).to be false expect(Dir.exist?(Rails.root.join("tmp", "specs", "update_directory"))).to be true end + + context 'when one file deletion fails' do + before do + allow(FileUtils).to receive(:rm).and_call_original + allow(FileUtils).to receive(:rm).with(Rails.root.join("tmp", "specs", "update_directory", 'NYPL_20210430_015000.zip').to_s).and_raise(Errno::ENOENT, 'No such file or directory @ apply2files') + end + it 'still deletes the other files that have not failed' do + described_class.perform_now + + expect(FileUtils).to have_received(:rm).exactly(4).times + expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'CUL_20210429_192300.zip'))).to be false + expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'HL_20210716_063500.zip'))).to be false + expect(File.file?(Rails.root.join("tmp", "specs", "update_directory", 'scsb_update_20240108_183400_1.xml'))).to be false + end + end end end