From f4252e27a67eddf3de20f07d74e512f84fa63269 Mon Sep 17 00:00:00 2001 From: Max Kadel Date: Tue, 24 Dec 2024 11:15:35 -0500 Subject: [PATCH] Move directory preparation into job --- app/jobs/scsb_import_full_job.rb | 16 +++++++++++----- app/models/scsb/partner_updates/full.rb | 1 - spec/jobs/scsb_import_full_job_spec.rb | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/jobs/scsb_import_full_job.rb b/app/jobs/scsb_import_full_job.rb index be876000..f25142e2 100644 --- a/app/jobs/scsb_import_full_job.rb +++ b/app/jobs/scsb_import_full_job.rb @@ -1,7 +1,7 @@ class ScsbImportFullJob include Sidekiq::Job def perform - delete_stale_files + prepare_directory Event.record do |event| event.save @@ -18,10 +18,16 @@ def created_dump(event) Dump.create!(dump_type: :partner_recap_full, event_id: event.id) end - def delete_stale_files - 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")) + def prepare_directory + update_directory = ENV['SCSB_PARTNER_UPDATE_DIRECTORY'] || '/tmp/updates' + FileUtils.mkdir_p(update_directory) + delete_stale_files(update_directory:) + end + + def delete_stale_files(update_directory:) + files_to_delete = Dir.glob("#{update_directory}/*.zip") + .concat(Dir.glob("#{update_directory}/*.xml")) + .concat(Dir.glob("#{update_directory}/*.csv")) files_to_delete.each do |file| FileUtils.rm file rescue Errno::ENOENT diff --git a/app/models/scsb/partner_updates/full.rb b/app/models/scsb/partner_updates/full.rb index 0c248526..f6205e2b 100644 --- a/app/models/scsb/partner_updates/full.rb +++ b/app/models/scsb/partner_updates/full.rb @@ -6,7 +6,6 @@ def initialize(dump:, dump_file_type:, timestamp: DateTime.now.to_time) end def process_full_files - prepare_directory download_and_process_full(inst: "NYPL", prefix: 'scsbfull_nypl_') download_and_process_full(inst: "CUL", prefix: 'scsbfull_cul_') download_and_process_full(inst: "HL", prefix: 'scsbfull_hl_') diff --git a/spec/jobs/scsb_import_full_job_spec.rb b/spec/jobs/scsb_import_full_job_spec.rb index 268c9270..4ba4ec89 100644 --- a/spec/jobs/scsb_import_full_job_spec.rb +++ b/spec/jobs/scsb_import_full_job_spec.rb @@ -17,6 +17,22 @@ expect(Scsb::PartnerUpdates).to have_received(:full) end + describe 'when there is no directory' do + let(:update_directory_path) { Rails.root.join("tmp", "specs", "update_directory") } + before do + FileUtils.rm_rf(update_directory_path) + + allow(ENV).to receive(:[]).and_call_original + allow(ENV).to receive(:[]).with('SCSB_PARTNER_UPDATE_DIRECTORY').and_return(update_directory_path) + + end + it 'creates the update directory' do + expect(Dir.exist?(update_directory_path)).to be false + described_class.perform_async + expect(Dir.exist?(update_directory_path)).to be true + end + end + describe 'when there are stale files in the update directory path' do let(:update_directory_path) { Rails.root.join("tmp", "specs", "update_directory") }