Skip to content

Commit

Permalink
Move directory preparation into job
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Dec 24, 2024
1 parent 87e77d9 commit f4252e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
16 changes: 11 additions & 5 deletions app/jobs/scsb_import_full_job.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ScsbImportFullJob
include Sidekiq::Job
def perform
delete_stale_files
prepare_directory

Event.record do |event|
event.save
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion app/models/scsb/partner_updates/full.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_')
Expand Down
16 changes: 16 additions & 0 deletions spec/jobs/scsb_import_full_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down

0 comments on commit f4252e2

Please sign in to comment.