diff --git a/umich_catalog_indexing/lib/jobs/translation_map_generator.rb b/umich_catalog_indexing/lib/jobs/translation_map_generator.rb index 92d22af3..4eadc5b5 100644 --- a/umich_catalog_indexing/lib/jobs/translation_map_generator.rb +++ b/umich_catalog_indexing/lib/jobs/translation_map_generator.rb @@ -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) diff --git a/umich_catalog_indexing/spec/jobs/translation_map_generator_spec.rb b/umich_catalog_indexing/spec/jobs/translation_map_generator_spec.rb index 88415454..dad959a8 100644 --- a/umich_catalog_indexing/spec/jobs/translation_map_generator_spec.rb +++ b/umich_catalog_indexing/spec/jobs/translation_map_generator_spec.rb @@ -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}`