Skip to content

Commit

Permalink
Add report emails to preservation workflow
Browse files Browse the repository at this point in the history
Why these changes are being introduced:

We found a bug in the preservation workflow last month that we
might have identified sooner if the preservation submission job
triggered a report email.

Relevant ticket(s):

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

How this addresses that need:

* Removes the Preservation Submission Prep Job
* Updates the Preservation Submission Job to accept an array of theses
* Adds a preservation results email to the Report Mailer that is
enqueued on completion of the Preservation Submission Job
* Updates the preservation rake task to convert the input thesis into
an array, so it will continue to work

Side effects of this change:

We not sure why we implemented the 'prep' job pattern: passing in
an array of theses so the actual job takes a single thesis as a
parameter. There is some risk in removing this job, since we don't
remember why it exists in the first place.
  • Loading branch information
jazairi committed Nov 9, 2023
1 parent 337eb35 commit 02d1fc1
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 56 deletions.
2 changes: 1 addition & 1 deletion app/jobs/dspace_publication_results_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform
results[:errors] << "Error reading from SQS queue: #{e}"
end

PreservationSubmissionPrepJob.perform_later(results[:preservation_ready]) if results[:preservation_ready].any?
PreservationSubmissionJob.perform_later(results[:preservation_ready]) if results[:preservation_ready].any?
MarcExportJob.perform_later(results[:marc_exports]) if results[:marc_exports].any?
ReportMailer.publication_results_email(results).deliver_now if results[:total].positive? ||
results[:errors].any?
Expand Down
26 changes: 17 additions & 9 deletions app/jobs/preservation_submission_job.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
class PreservationSubmissionJob < ActiveJob::Base
queue_as :default

def perform(thesis)
Rails.logger.info("Thesis #{thesis.id} is now being prepared for preservation")
sip = thesis.submission_information_packages.create
preserve_sip(sip)
Rails.logger.info("Thesis #{thesis.id} has been sent to preservation")
rescue StandardError, Aws::Errors => e
Rails.logger.info("Thesis #{thesis.id} could not be preserved: #{e}")
sip.preservation_status = 'error'
sip.save
def perform(theses)
Rails.logger.info("Preparing to send #{theses.count} theses to preservation")
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
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?
end

private
Expand Down
11 changes: 0 additions & 11 deletions app/jobs/preservation_submission_prep_job.rb

This file was deleted.

10 changes: 10 additions & 0 deletions app/mailers/report_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ def publication_results_email(results)
cc: ENV['MAINTAINER_EMAIL'],
subject: 'DSpace publication results summary')
end

def preservation_results_email(results)
return unless ENV.fetch('DISABLE_ALL_EMAIL', 'true') == 'false' # allows PR builds to disable emails

@results = results
mail(from: "MIT Libraries <#{ENV['ETD_APP_EMAIL']}>",
to: ENV['THESIS_ADMIN_EMAIL'],
cc: ENV['MAINTAINER_EMAIL'],
subject: 'Archivematica preservation submission results summary')
end
end
21 changes: 21 additions & 0 deletions app/views/report_mailer/preservation_results_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<p>Hello,</p>

<p>Below is a report of the Archivematica preservation submission job.</p>

<p>Summary of results:</p>

<ul>
<li>Total theses queued for preservation: <%= @results[:total] %></li>
<li>Total theses sent to preservation: <%= @results[:processed] %></li>
<li>Errors found: <%= @results[:errors].count %></li>
</ul>

<% if @results[:errors].any? %>
<p>The following errors require processor attention:</p>

<ul>
<% @results[:errors].each do |e| %>
<li><%= e %></li>
<% end %>
</ul>
<% end %>
3 changes: 2 additions & 1 deletion lib/tasks/preservation.rake
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
namespace :preservation do
desc 'Sends a single thesis to preservation'
task :preserve_thesis_by_id, [:thesis_id] => :environment do |_t, args|
return Rails.logger.info('.')
if args.thesis_id
Rails.logger.info("Attempting to send #{args.thesis_id} to preservation...")
thesis = Thesis.find(args.thesis_id)

# Only published theses may be sent to preservation. We already check for this in SubmissionInformationPackage
# validations, but double-checking here to save potential confusion.
if thesis.publication_status == 'Published'
PreservationSubmissionJob.perform_now(thesis)
PreservationSubmissionJob.perform_now([thesis])
else
Rails.logger.info("Thesis status of #{thesis.publication_status} cannot be preserved.")
end
Expand Down
2 changes: 1 addition & 1 deletion test/jobs/dspace_publication_results_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def teardown
end

test 'enqueues preservation submission prep job' do
assert_enqueued_with(job: PreservationSubmissionPrepJob) do
assert_enqueued_with(job: PreservationSubmissionJob) do
DspacePublicationResultsJob.perform_now
end
end
Expand Down
28 changes: 0 additions & 28 deletions test/jobs/preservation_submission_job_prep_test.rb

This file was deleted.

22 changes: 17 additions & 5 deletions test/jobs/preservation_submission_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,50 @@ def setup_thesis
thesis
end

test 'sends report emails' do
ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do
assert_difference('ActionMailer::Base.deliveries.size', 1) do
PreservationSubmissionJob.perform_now([setup_thesis])
end
end
end

test 'creates a SIP' do
thesis = setup_thesis
assert_equal 0, thesis.submission_information_packages.count

PreservationSubmissionJob.perform_now(thesis)
PreservationSubmissionJob.perform_now([thesis])
assert_equal 1, thesis.submission_information_packages.count
end

test 'creates multiple SIPs' do
theses = [setup_thesis, theses(:published)]
end

test 'updates preservation_status to "preserved" after successfully processing a thesis' do
thesis = setup_thesis
PreservationSubmissionJob.perform_now(thesis)
PreservationSubmissionJob.perform_now([thesis])
assert_equal 'preserved', thesis.submission_information_packages.last.preservation_status
end

test 'updates preserved_at to the current time after successfully processing a thesis' do
time = DateTime.new.getutc
Timecop.freeze(time) do
thesis = setup_thesis
PreservationSubmissionJob.perform_now(thesis)
PreservationSubmissionJob.perform_now([thesis])
assert_equal time, thesis.submission_information_packages.last.preserved_at
end
end

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

test 'does not update preserved_at if the job enters an error state' do
thesis = theses(:one)
PreservationSubmissionJob.perform_now(thesis)
PreservationSubmissionJob.perform_now([thesis])
assert_nil thesis.submission_information_packages.last.preserved_at
end
end
19 changes: 19 additions & 0 deletions test/mailers/report_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,23 @@ class ReportMailerTest < ActionMailer::TestCase
assert_match 'Couldn&#39;t find Thesis with &#39;id&#39;=9999999999999', email.body.to_s
end
end

test 'sends reports for preservation submission results' do
ClimateControl.modify DISABLE_ALL_EMAIL: 'false' do
results = { total: 2, processed: 1, errors: ["Couldn't find Thesis with 'id'=9999999999999"] }
email = ReportMailer.preservation_results_email(results)

assert_emails 1 do
email.deliver_now
end

assert_equal ['app@example.com'], email.from
assert_equal ['test@example.com'], email.to
assert_equal 'Archivematica preservation submission results summary', email.subject.to_s
assert_match 'Total theses queued for preservation: 2', email.body.to_s
assert_match 'Total theses sent to preservation: 1', email.body.to_s
assert_match 'Errors found: 1', email.body.to_s
assert_match 'Couldn&#39;t find Thesis with &#39;id&#39;=9999999999999', email.body.to_s
end
end
end

0 comments on commit 02d1fc1

Please sign in to comment.