Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report emails to preservation workflow #1248

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still breaks my brain every time I see it. I still can't parse what it does, but only know it works because it works.


@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 %>
2 changes: 1 addition & 1 deletion lib/tasks/preservation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace :preservation do
# 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
Loading