-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests and WorkflowErrorsReporter class
- Loading branch information
Showing
7 changed files
with
96 additions
and
57 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 |
---|---|---|
@@ -1,45 +1,49 @@ | ||
require 'faraday' | ||
require 'retries' | ||
# This class will send a put ReST call to the preservationWF via faraday. This also parses | ||
# out a shorter error message, that only includes the moab validation error. | ||
# send errors to preservationWF workflow for an object via ReST calls. | ||
class WorkflowErrorsReporter | ||
|
||
def self.update_workflow(druid, error_message) | ||
error_message.select { |error| request(druid, 'moab-valid', moab_error(error)) if error.key?(PreservedObjectHandlerResults::INVALID_MOAB) } | ||
def self.update_workflow(druid, process_name, error_message) | ||
response = http_workflow_request(druid, process_name, error_message) | ||
|
||
# had to add this if response becuase test for making sure if Setting.workflow_service_url | ||
# was empty returned the correct Rails.logger.warn | ||
|
||
if response | ||
if response.status == 204 | ||
Rails.logger.debug("put request went through") | ||
else | ||
# didn't add a status == 400 because either way it is getting handled in the | ||
# rescue, if i add the ==400 then there is double logging. | ||
Rails.logger.warn("put request did not go through") | ||
end | ||
end | ||
rescue Faraday::Error, StandardError => e | ||
Rails.logger.warn(e) | ||
end | ||
|
||
def self.request(druid, process_name, error_message) | ||
private_class_method def self.http_workflow_request(druid, process_name, error_message) | ||
return unless conn | ||
handler = proc do |exception, attempt_number, total_delay| | ||
Rails.logger.warn("Handler saw a #{exception.class}; retry attempt #{attempt_number}; #{total_delay} seconds have passed.") | ||
Rails.logger.debug("Handler saw a #{exception.class}; retry attempt #{attempt_number}; #{total_delay} seconds have passed.") | ||
end | ||
with_retries(max_tries: 3, handler: handler, rescue: workflow_service_exceptions_to_catch) do | ||
@connection.put do |request| | ||
with_retries(max_tries: 3, handler: handler, rescue: Faraday::Error) do | ||
conn.put do |request| | ||
request.headers['content-type'] = "application/xml" | ||
request.url "/workflow/dor/objects/druid:#{druid}/workflows/preservationWF/#{process_name}" | ||
request.body = "<process name='#{process_name}' status='error' errorMessage='#{error_message}'/>" | ||
request.body = "<process name='#{process_name} status='error' errorMessage='#{error_message}'/>" | ||
end | ||
end | ||
rescue *workflow_service_exceptions_to_catch => e | ||
raise Faraday::Error, e | ||
end | ||
|
||
private_class_method def self.conn | ||
if Settings.workflow_services.url.present? | ||
@connection ||= Faraday.new(url: Settings.workflow_services.url) do |c| | ||
if Settings.workflow_services_url.present? | ||
@@connection ||= Faraday.new(url: Settings.workflow_services_url) do |c| | ||
c.use Faraday::Response::RaiseError | ||
c.use Faraday::Adapter::NetHttp | ||
end | ||
else | ||
Rails.logger.warn('no workflow hookup - assume you are in test or dev environment') | ||
end | ||
@connection | ||
end | ||
private_class_method def self.workflow_service_exceptions_to_catch | ||
[Faraday::Error] | ||
end | ||
|
||
private_class_method def self.moab_error(error) | ||
/^.*\)(.*)$/.match(error[PreservedObjectHandlerResults::INVALID_MOAB]).captures.first | ||
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 |
---|---|---|
|
@@ -38,5 +38,4 @@ preservation_policies: | |
provlog: | ||
enable: false | ||
|
||
workflow_services: | ||
url: '' | ||
workflow_services_url: '' |
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 |
---|---|---|
@@ -1,42 +1,54 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe WorkflowErrorsReporter do | ||
describe '.update_workflow(druid, error_message)' do | ||
let(:body) { "<process name='moab-valid' status='error' errorMessage=' Invalid moab, validation errors: [\"Should contain only sequential version directories. Current directories: [\\\"v0001\\\", \\\"v0002\\\", \\\"v0009\\\"]\"]'/>" } | ||
let(:error_msg) do | ||
[{ 10 => "PreservedObjectHandler(jj925bx9565, 9, 6570668, <Endpoint:...>) PreservedObject db object does not exist" }, | ||
{ 13 => "PreservedObjectHandler(jj925bx9565, 9, 6570668, <Endpoint:...>) Invalid moab, validation errors: [\"Should contain only sequential version directories. Current directories: [\\\"v0001\\\", \\\"v0002\\\", \\\"v0009\\\"]\"]" }] | ||
end | ||
let(:put) { 'https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid' } | ||
let(:headers) { { 'Content-Type' => 'application/xml' } } | ||
let(:result_array) do | ||
{ 13 => "Invalid moab, validation error...ential version directories." } | ||
end | ||
let(:body) { "<process name='moab-valid status='error' errorMessage='{13=>\"Invalid moab, validation error...ential version directories.\"}'/>" } | ||
|
||
describe '.update_workflow with 204' do | ||
before do | ||
Settings.workflow_services.url = 'https://sul-lyberservices-test.stanford.edu/workflow/' | ||
stub_request(:put, 'https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid') | ||
.with(body: body, | ||
headers: { 'Content-Type' => 'application/xml' }) | ||
.to_return(status: 204, body: "", headers: {}) | ||
Settings.workflow_services_url = 'https://sul-lyberservices-test.stanford.edu/workflow/' | ||
stub_request(:put, put) | ||
.with(body: body, | ||
headers: headers) | ||
.to_return(status: 204, body: "", headers: {}) | ||
end | ||
it 'updates preservationWF with invalid moab errors' do | ||
expect(described_class.update_workflow('jj925bx9565', error_msg)).to eq [error_msg[1]] | ||
|
||
it 'returns a 204 response status' do | ||
expect(Rails.logger).to receive(:debug).with("put request went through") | ||
described_class.update_workflow('jj925bx9565', 'moab-valid', result_array) | ||
end | ||
end | ||
describe '.request' do | ||
it 'rescues for Faraday::Error' do | ||
allow(described_class).to receive(:request).and_raise(Faraday::Error) | ||
expect { described_class.request('jj925bx9565', 'moab-valid', 'Foo() error') }.to raise_error(Faraday::Error) | ||
|
||
describe '.update_workflow with 400' do | ||
before do | ||
Settings.workflow_services_url = 'https://sul-lyberservices-test.stanford.edu/workflow/' | ||
stub_request(:put, put) | ||
.with(body: body, | ||
headers: headers) | ||
.to_return(status: 400, body: "", headers: {}) | ||
end | ||
|
||
it 'returns a 400 response status' do | ||
expect(Rails.logger).to receive(:warn).with(Faraday::ClientError) | ||
described_class.update_workflow('jj925bx9565', 'moab-valid', result_array) | ||
end | ||
end | ||
|
||
describe '.update_workflow with invalid workflow_services.url' do | ||
before do | ||
stub_request(:put, "https://sul-lyberservices-test.stanford.edu/workflow/dor/objects/druid:jj925bx9565/workflows/preservationWF/moab-valid") | ||
.with(body: "<process name='moab-valid' status='error' errorMessage=' Invalid Moab'/>", | ||
headers: { 'Content-Type' => 'application/xml' }) | ||
.to_return(status: 200, body: "", headers: {}) | ||
stub_request(:put, put) | ||
.with(body: body, | ||
headers: headers) | ||
.to_return(status: 400, body: "", headers: {}) | ||
end | ||
it 'returns Rails warning' do | ||
Settings.workflow_services.url = '' | ||
Settings.workflow_services_url = '' | ||
expect(Rails.logger).to receive(:warn).with('no workflow hookup - assume you are in test or dev environment') | ||
described_class.update_workflow('jj925bx9565', [{ 13 => "Foo() Invalid Moab" }]) | ||
described_class.update_workflow('jj925bx9565', 'moab-valid', result_array) | ||
end | ||
end | ||
end |