-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why these changes are being introduced: We need a way to initiate SIP creation, bagging and streaming to S3. Relevant ticket(s): https://mitlibraries.atlassian.net/browse/ETD-553 How this addresses that need: This creates two jobs: 1. PreservationSubmissionJob attempts to send a single thesis to preservation and modifies the preservation_status and preserved_at fields accordingly. 2. PreservationSubmissionPrepJob loops through an array of theses to preserve. Side effects of this change: * preservation_status is now an enum with three options: unpreserved (the default), preserved, and error. * Adds `preservation.rake` task to send a single thesis to preservation via Rails CLI.
- Loading branch information
Showing
9 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class PreservationSubmissionJob < ActiveJob::Base | ||
queue_as :default | ||
|
||
def perform(thesis) | ||
begin | ||
Rails.logger.info("Thesis #{thesis.id} is now being prepared for preservation") | ||
sip = thesis.submission_information_packages.create | ||
SubmissionInformationPackageZipper.new(sip) | ||
sip.preservation_status = 'preserved' | ||
sip.preserved_at = DateTime.now | ||
sip.save | ||
Rails.logger.info("Thesis #{thesis.id} has been sent to preservation") | ||
rescue StandardError, NoMethodError, Aws::Errors => e | ||
Rails.logger.info ("Thesis #{thesis.id} could not be preserved: #{e}") | ||
sip.preservation_status = 'error' | ||
sip.save | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class PreservationSubmissionPrepJob < ActiveJob::Base | ||
queue_as :default | ||
|
||
def perform(theses) | ||
Rails.logger.info("Preparing to send #{theses.count} theses to preservation") | ||
|
||
theses.each do |thesis| | ||
PreservationSubmissionJob.perform_later(thesis) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace :preservation do | ||
desc 'Sends a single thesis to preservation' | ||
task :preserve_thesis_by_id, [:thesis_id] => :environment do |_t, args| | ||
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) | ||
else | ||
Rails.logger.info("Thesis status of #{thesis.publication_status} cannot be preserved.") | ||
end | ||
else | ||
Rails.logger.info('No thesis ID provided.') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'test_helper' | ||
|
||
class PreservationSubmissionPrepJobTest < ActiveJob::TestCase | ||
|
||
test 'queues 1 job for 1 thesis' do | ||
theses = [theses(:one)].to_a | ||
|
||
assert_enqueued_jobs 1 do | ||
PreservationSubmissionPrepJob.perform_now(theses) | ||
end | ||
end | ||
|
||
test 'queues 2 jobs for 2 theses' do | ||
theses = [theses(:one), theses(:two)].to_a | ||
|
||
assert_enqueued_jobs 2 do | ||
PreservationSubmissionPrepJob.perform_now(theses) | ||
end | ||
end | ||
|
||
test 'queues same number of theses it receives' do | ||
theses = Thesis.in_review.to_a | ||
|
||
assert_enqueued_jobs theses.count do | ||
PreservationSubmissionPrepJob.perform_now(theses) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'test_helper' | ||
|
||
class PreservationSubmissionJobTest < ActiveJob::TestCase | ||
|
||
# because we need to actually use the file it's easier to attach it in the test rather | ||
# than use our fixtures as the fixtures oddly don't account for the file actually being | ||
# where ActiveStorage expects them to be. We also need this to be a record that looks like | ||
# a published record so we'll use the published fixture, remove the fixtured files, and attach | ||
# one again. | ||
def setup_thesis | ||
thesis = theses(:published) | ||
thesis.files = [] | ||
thesis.save | ||
file = Rails.root.join('test', 'fixtures', 'files', 'registrar_data_small_sample.csv') | ||
thesis.files.attach(io: File.open(file), filename: 'registrar_data_small_sample.csv') | ||
thesis | ||
end | ||
|
||
test 'creates a SIP' do | ||
thesis = setup_thesis | ||
assert_equal 0, thesis.submission_information_packages.count | ||
|
||
PreservationSubmissionJob.perform_now(thesis) | ||
assert_equal 1, thesis.submission_information_packages.count | ||
end | ||
|
||
test 'updates preservation_status to "preserved" after successfully processing a thesis' do | ||
thesis = setup_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.now.getutc | ||
Timecop.freeze(time) do | ||
thesis = setup_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) | ||
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) | ||
assert_nil thesis.submission_information_packages.last.preserved_at | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters