Skip to content

Commit

Permalink
Merge pull request #944 from OregonDigital/migration_fix_task
Browse files Browse the repository at this point in the history
Migration fix task
  • Loading branch information
luisgreg99 authored Jan 14, 2020
2 parents 594f32f + 547c6f4 commit 7add98b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/tasks/migration/fixes.rake
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 added spec/fixtures/batch1/abcde1234.zip
Binary file not shown.
Binary file added spec/fixtures/batch1/abcde5678.zip
Binary file not shown.
38 changes: 38 additions & 0 deletions spec/tasks/migration/rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]') }
Expand Down Expand Up @@ -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

0 comments on commit 7add98b

Please sign in to comment.