-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #944 from OregonDigital/migration_fix_task
Migration fix task
- Loading branch information
Showing
6 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :migration do | ||
namespace :fixes do | ||
desc 'Fix batch by retrying file attach' | ||
task retry_file_attach: :environment do | ||
batch = ENV['batch'] | ||
|
||
batch_path = File.join(Hyrax::Migrator.config.ingest_local_path, batch) | ||
|
||
pids = Dir.entries(batch_path) | ||
.select { |e| File.file?(File.join(batch_path, e)) && File.extname(e) == '.zip' } | ||
.map { |zip_file| File.basename(zip_file, File.extname(zip_file)) } | ||
|
||
pids.each do |pid| | ||
assess(pid) | ||
end | ||
end | ||
|
||
desc 'Fix one asset by retrying file attach' | ||
task retry_single_file_attach: :environment do | ||
pid = ENV['pid'] | ||
assess(pid) | ||
end | ||
end | ||
end | ||
|
||
def assess(pid) | ||
af_object = ActiveFedora::Base.exists?(pid) ? ActiveFedora::Base.find(pid) : nil | ||
attach_file(af_object) if !af_object.nil? && af_object.representative_id.nil? | ||
end | ||
|
||
# Looks for the content file in the file_system_path | ||
# TODO Enable retrieving content file from the zip? | ||
def attach_file(af_object) | ||
filename = files.find { |f| f.downcase.start_with? af_object.id } | ||
puts "Unable to find content file for #{af_object.id}" && return if filename.nil? | ||
|
||
puts "Enqueueing job for #{af_object.id}" | ||
AttachFilesToWorkJob.perform_later(af_object, [upload_file(migration_user, filename)]) | ||
end | ||
|
||
def migration_user | ||
@migration_user ||= User.where(email: Hyrax::Migrator.config.migration_user).first | ||
end | ||
|
||
def files | ||
@files ||= Dir.entries(Hyrax::Migrator.config.file_system_path) | ||
end | ||
|
||
def upload_file(migration_user, filename) | ||
file = File.open(File.join(Hyrax::Migrator.config.file_system_path, filename)) | ||
uploaded_file = Hyrax::UploadedFile.new(user: migration_user, file: file) | ||
uploaded_file.save | ||
uploaded_file | ||
end |
Empty file.
Empty file.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
require 'pry' | ||
|
||
RSpec.describe 'Rake tasks' do | ||
include ActiveJob::TestHelper | ||
describe 'migration:bulk_approve' do | ||
let(:work1) { build(:work, user: other_user, state: state) } | ||
let(:migration_user) { User.new(email: '[email protected]') } | ||
|
@@ -71,4 +72,41 @@ | |
end | ||
end | ||
end | ||
|
||
describe 'migration:fixes:retry_file_attach' do | ||
context 'when there is a batch of persisted assets with no files' do | ||
let(:migration_user) { User.new(email: '[email protected]') } | ||
let(:work1) { build(:work, user: migration_user, id: 'abcde1234') } | ||
let(:work2) { build(:work, user: migration_user, id: 'abcde5678') } | ||
let(:path) { Rails.root.join 'spec/fixtures' } | ||
let(:upload_file) { Hyrax::UploadedFile.new(user: migration_user, file: file) } | ||
let(:file) { File.open(File.join(path, 'abcde1234_content.txt')) } | ||
let(:run_rake_task) do | ||
ENV['batch'] = 'batch1' | ||
Rake.application.invoke_task 'migration:fixes:retry_file_attach' | ||
end | ||
|
||
before do | ||
work1.save | ||
work2.save | ||
migration_user.save | ||
upload_file.save | ||
Hyrax::Migrator.config.ingest_local_path = path | ||
Hyrax::Migrator.config.file_system_path = path | ||
Rake.application.rake_require 'tasks/migration/fixes' | ||
Rake::Task.define_task(:environment) | ||
allow(Hyrax::UploadedFile).to receive(:new).and_return(upload_file) | ||
run_rake_task | ||
end | ||
|
||
after do | ||
clear_enqueued_jobs | ||
clear_performed_jobs | ||
end | ||
|
||
it 'enqueues jobs' do | ||
expect(ActiveJob::Base.queue_adapter.enqueued_jobs.size).to eq 2 | ||
end | ||
end | ||
end | ||
end |