From a0caeca94ef0998e370ab671e697473e58b4fca1 Mon Sep 17 00:00:00 2001 From: Adam Jazairi Date: Tue, 22 Feb 2022 16:19:48 -0500 Subject: [PATCH] Guard against nil tempfiles in MARC export job Why these changes are being introduced: If a tempfile has already been closed and unlinked, the `ensure` block in MarcExportJob#perform will throw an error. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/ETD-546 How this addresses that need: This checks if the tempfile exists before attempting to close it. Side effects of this change: Adds rubyzip to gemfile. Rubyzip is already used by other gems, and it can be called in local development, but bootsnap seems to require it to be loaded explicitly. --- Gemfile | 1 + Gemfile.lock | 1 + app/jobs/marc_export_job.rb | 2 +- app/models/marc_batch.rb | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index ede4980e..2060b4a3 100644 --- a/Gemfile +++ b/Gemfile @@ -23,6 +23,7 @@ gem 'omniauth-saml' gem 'paper_trail' gem 'puma' gem 'rails', '~> 6.1.0' +gem 'rubyzip' gem 'sass-rails' gem 'sentry-rails' gem 'sentry-ruby' diff --git a/Gemfile.lock b/Gemfile.lock index 2e186a39..159a2347 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -404,6 +404,7 @@ DEPENDENCIES rails (~> 6.1.0) rubocop rubocop-rails + rubyzip sass-rails scout_apm selenium-webdriver diff --git a/app/jobs/marc_export_job.rb b/app/jobs/marc_export_job.rb index 5700bacc..4e5d9b09 100644 --- a/app/jobs/marc_export_job.rb +++ b/app/jobs/marc_export_job.rb @@ -8,7 +8,7 @@ def perform(theses) zip_file = MarcBatch.new(theses, marc_filename, zip_filename).build BatchMailer.marc_batch_email(zip_filename, zip_file, theses).deliver_now ensure - zip_file.close + zip_file&.close end end diff --git a/app/models/marc_batch.rb b/app/models/marc_batch.rb index b3bfdbcd..6dcdadad 100644 --- a/app/models/marc_batch.rb +++ b/app/models/marc_batch.rb @@ -1,3 +1,5 @@ +require 'zip' + class MarcBatch def initialize(theses, marc_filename, zip_filename) @theses = theses