Skip to content

Commit

Permalink
updates translation map generator to take a force option
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Oct 4, 2024
1 parent e2a6344 commit 9e5896d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 13 additions & 9 deletions umich_catalog_indexing/lib/jobs/translation_map_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ def generate_all(dir: translation_map_directory)
end
end

def generate(generator:, dir: translation_map_directory)
def generate(generator:, dir: translation_map_directory, force: false)
S.logger.info "fetching #{generator.name}"
path = File.join(dir, generator.file_path)
if _should_fetch?(path)
temporary_path = "#{path}_#{SecureRandom.alphanumeric(8)}.temporary"
generator.write_to_file(temporary_path)
raise StandardError, "#{temporary_path} does not exist; Failed to load file" if !File.exist?(temporary_path)
raise StandardError, "#{temporary_path} is too small; Failed to load file" if File.size?(temporary_path) < 15
File.rename(temporary_path, path)
S.logger.info "updated #{path}"
else
if _should_not_fetch?(path) && force == false
S.logger.info "#{path} is less than one day old. Did not update"
return
end
temporary_path = "#{path}_#{SecureRandom.alphanumeric(8)}.temporary"
generator.write_to_file(temporary_path)
raise StandardError, "#{temporary_path} does not exist; Failed to load file" if !File.exist?(temporary_path)
raise StandardError, "#{temporary_path} is too small; Failed to load file" if File.size?(temporary_path) < 15
File.rename(temporary_path, path)
S.logger.info "updated #{path}"
end

def _should_not_fetch?(file)
!_should_fetch?(file)
end

def _should_fetch?(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def write_to_file(path)
expect(File.size?(tm_path)).to be_nil
end
end
context "force param is true" do
it "generates a new file" do
@params[:force] = true
`touch #{tm_path}`
subject
expect(File.size?(tm_path)).to eq(20)
end
end
context "has old translation map files" do
it "generates new files" do
`touch -d "-2 days" #{tm_path}`
Expand Down

0 comments on commit 9e5896d

Please sign in to comment.