Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish objects using a background job
Objects that have several hundred files may take several minutes to publish, so make this an asynchronous job. In our testing we found a 600 page book took over 82s to publish
- Loading branch information
Showing
13 changed files
with
141 additions
and
71 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
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
# Create virtual objects in the background | ||
class PublishJob < ApplicationJob | ||
queue_as :default | ||
|
||
# @param [String] druid the identifier of the item to be published | ||
# @param [BackgroundJobResult] background_job_result identifier of a background job result to store status info | ||
def perform(druid:, background_job_result:) | ||
background_job_result.processing! | ||
|
||
errors = [] | ||
|
||
begin | ||
item = Dor.find(druid) | ||
PublishMetadataService.publish(item) | ||
rescue Dor::DataError => e | ||
errors << { title: 'Data error', detail: e.message } | ||
end | ||
|
||
background_job_result.output = { errors: errors } if errors.any? | ||
|
||
background_job_result.complete! | ||
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
json.output @output | ||
json.status @status | ||
json.output @result.output | ||
json.status @result.status |
5 changes: 5 additions & 0 deletions
5
db/migrate/20191015193638_remove_background_job_results_code.rb
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,5 @@ | ||
class RemoveBackgroundJobResultsCode < ActiveRecord::Migration[5.2] | ||
def change | ||
remove_column :background_job_results, :code | ||
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
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 |
---|---|---|
|
@@ -4,6 +4,5 @@ | |
factory :background_job_result do | ||
output { {} } | ||
status { 'pending' } | ||
code { 202 } | ||
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe PublishJob, type: :job do | ||
subject(:perform) { described_class.perform_now(druid: druid, background_job_result: result) } | ||
|
||
let(:druid) { 'druid:mk420bs7601' } | ||
let(:result) { create(:background_job_result) } | ||
let(:item) { instance_double(Dor::Item) } | ||
|
||
before do | ||
allow(Dor).to receive(:find).with(druid).and_return(item) | ||
allow(result).to receive(:processing!) | ||
end | ||
|
||
context 'with no errors' do | ||
before do | ||
allow(PublishMetadataService).to receive(:publish) | ||
perform | ||
end | ||
|
||
it 'marks the job as processing' do | ||
expect(result).to have_received(:processing!).once | ||
end | ||
|
||
it 'invokes the PublishMetadataService' do | ||
expect(PublishMetadataService).to have_received(:publish).with(item).once | ||
end | ||
|
||
it 'marks the job as complete' do | ||
expect(result).to be_complete | ||
end | ||
|
||
it 'has no output' do | ||
expect(result.output).to be_blank | ||
end | ||
end | ||
|
||
context 'with errors returned by PublishMetadataService' do | ||
let(:error_message) { "DublinCoreService#ng_xml produced incorrect xml (no children):\n<xml/>" } | ||
|
||
before do | ||
allow(PublishMetadataService).to receive(:publish).and_raise(Dor::DataError, error_message) | ||
perform | ||
end | ||
|
||
it 'marks the job as processing' do | ||
expect(result).to have_received(:processing!).once | ||
end | ||
|
||
it 'invokes the PublishMetadataService' do | ||
expect(PublishMetadataService).to have_received(:publish).with(item).once | ||
end | ||
|
||
it 'marks the job as complete' do | ||
expect(result).to be_complete | ||
end | ||
|
||
it 'has output with errors' do | ||
expect(result.output[:errors]).to eq [{ 'detail' => error_message, 'title' => 'Data error' }] | ||
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