Skip to content

Commit

Permalink
Refactor error handling in Preservation Submission Job
Browse files Browse the repository at this point in the history
Why these changes are being introduced:

The Preservation Submission Job currently handles exceptions
during SIP creation by setting the SIP's status to 'error' and
saving it. This allows us to see failed SIPs in the administrate
dashboard, but it also causes preservation to fail silently.

Relevant ticket(s):

https://mitlibraries.atlassian.net/browse/ETD-647

How this addresses that need:

This refactors the Preservation Submission Job to create SIPs
using `create!` instead of `create`, so that exceptions are
raised during the object creation. When exceptions are raised,
they are logged and added to the `results` array. Since no
SIP is created when an exception is raised by `create!`, the
logic in the `rescue` block around updating `preservation_status`
has been removed.

Side effects of this change:

* Fixtures have been adjusted to accommodate testing.
* A quasi-related test that had no assertions has been updated to
actually test something.
* The `preservation_status` of 'error' has been removed, as it
is no longer used in the app. There are currently no SIPs in
production that have this status.
* Unrelated to this change, the `baggable` module has been moved
from `app/models` to `app/lib`. This to be consistent with
where we store our other module, and to make it clear that it is
not a model.
  • Loading branch information
jazairi committed Nov 21, 2023
1 parent 7c1ad59 commit 4c3f088
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
4 changes: 1 addition & 3 deletions app/jobs/preservation_submission_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ def perform(theses)
results = { total: theses.count, processed: 0, errors: [] }
theses.each do |thesis|
Rails.logger.info("Thesis #{thesis.id} is now being prepared for preservation")
sip = thesis.submission_information_packages.create
sip = thesis.submission_information_packages.create!
preserve_sip(sip)
Rails.logger.info("Thesis #{thesis.id} has been sent to preservation")
results[:processed] += 1
rescue StandardError, Aws::Errors => e
preservation_error = "Thesis #{thesis.id} could not be preserved: #{e}"
Rails.logger.info(preservation_error)
sip.preservation_status = 'error'
sip.save
results[:errors] << preservation_error
end
ReportMailer.preservation_results_email(results).deliver_now if results[:total].positive?
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion app/models/submission_information_package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SubmissionInformationPackage < ApplicationRecord

before_create :set_metadata, :set_bag_declaration, :set_manifest, :set_bag_name

enum preservation_status: %i[unpreserved preserved error]
enum preservation_status: %i[unpreserved preserved]

def data
file_locations = {}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/archivematica_accessions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ june_2018_001:
june_2021_001:
accession_number: '2021_001'
degree_period: june_2021

june_2022_001:
accession_number: '2022_001'
degree_period: june_2022
4 changes: 4 additions & 0 deletions test/fixtures/degree_periods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ june_2018:
june_2021:
grad_month: 'June'
grad_year: '2021'

june_2022:
grad_month: 'June'
grad_year: '2022'
1 change: 1 addition & 0 deletions test/fixtures/theses.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ engineer:
metadata_complete: true
issues_found: false
publication_status: Published
dspace_handle: '3456/7890'
proquest_exported: 'Full harvest'

long_abstracts_are_fun:
Expand Down
32 changes: 25 additions & 7 deletions test/jobs/preservation_submission_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ def setup_thesis
end

test 'creates multiple SIPs' do
theses = [setup_thesis, theses(:published)]
thesis_one = setup_thesis
thesis_two = theses(:engineer)
assert_equal 0, thesis_one.submission_information_packages.count
assert_equal 0, thesis_two.submission_information_packages.count

theses = [thesis_one, thesis_two]
PreservationSubmissionJob.perform_now(theses)
assert_equal 1, thesis_one.submission_information_packages.count
assert_equal 1, thesis_two.submission_information_packages.count

PreservationSubmissionJob.perform_now(theses)
assert_equal 2, thesis_one.submission_information_packages.count
assert_equal 2, thesis_two.submission_information_packages.count
end

test 'updates preservation_status to "preserved" after successfully processing a thesis' do
Expand All @@ -51,15 +63,21 @@ def setup_thesis
end
end

test 'rescues exceptions by updating preservation_status to "error"' do
thesis = theses(:one)
PreservationSubmissionJob.perform_now([thesis])
assert_equal 'error', thesis.submission_information_packages.last.preservation_status
test 'throws exceptions when a thesis is unbaggable' do
assert_raises StandardError do
PreservationSubmissionJob.perform_now([theses[:one]])
end

assert_nothing_raised do
PreservationSubmissionJob.perform_now([setup_thesis])
end
end

test 'does not update preserved_at if the job enters an error state' do
test 'does not create a SIP if the job enters an error state' do
thesis = theses(:one)
assert_empty thesis.submission_information_packages

PreservationSubmissionJob.perform_now([thesis])
assert_nil thesis.submission_information_packages.last.preserved_at
assert_empty thesis.submission_information_packages
end
end

0 comments on commit 4c3f088

Please sign in to comment.