Skip to content

Commit

Permalink
Moved the total failure code into the job class
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshingYou1 committed Sep 6, 2024
1 parent a8b5748 commit 7652af6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 82 deletions.
21 changes: 18 additions & 3 deletions app/sidekiq/hca/ezr_submission_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ module HCA
class EzrSubmissionJob
include Sidekiq::Job
include SentryLogging
include Common::Client::Concerns::Monitoring
VALIDATION_ERROR = HCA::SOAPParser::ValidationError

sidekiq_options retry: 14

sidekiq_retries_exhausted do |msg, _e|
Form1010Ezr::Service.new(nil).log_exhausted_submission_failure(
decrypt_form(msg['args'][0])
)
parsed_form = decrypt_form(msg['args'][0])

StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.failed_wont_retry")

if parsed_form.present?
PersonalInformationLog.create!(
data: parsed_form,
error_class: 'Form1010Ezr FailedWontRetry'
)

new.log_message_to_sentry(
'1010EZR total failure',
:error,
Form1010Ezr::Service.new(nil).veteran_initials(parsed_form),
ezr: :total_failure
)
end
end

def self.decrypt_form(encrypted_form)
Expand Down
31 changes: 6 additions & 25 deletions lib/form1010_ezr/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,12 @@ def log_submission_failure(parsed_form)
end
end

# When a submission runs out of retry attempts
def log_exhausted_submission_failure(parsed_form)
StatsD.increment("#{Form1010Ezr::Service::STATSD_KEY_PREFIX}.failed_wont_retry")

if parsed_form.present?
PersonalInformationLog.create!(
data: parsed_form,
error_class: 'Form1010Ezr FailedWontRetry'
)

log_message_to_sentry(
'1010EZR total failure',
:error,
veteran_initials(parsed_form),
ezr: :total_failure
)
end
def veteran_initials(parsed_form)
{
first_initial: parsed_form.dig('veteranFullName', 'first')&.chr || 'no initial provided',
middle_initial: parsed_form.dig('veteranFullName', 'middle')&.chr || 'no initial provided',
last_initial: parsed_form.dig('veteranFullName', 'last')&.chr || 'no initial provided'
}
end

private
Expand Down Expand Up @@ -193,13 +182,5 @@ def log_and_raise_error(error, form)
Rails.logger.error "10-10EZR form submission failed: #{error.message}"
raise error
end

def veteran_initials(parsed_form)
{
first_initial: parsed_form.dig('veteranFullName', 'first')&.chr || 'no initial provided',
middle_initial: parsed_form.dig('veteranFullName', 'middle')&.chr || 'no initial provided',
last_initial: parsed_form.dig('veteranFullName', 'last')&.chr || 'no initial provided'
}
end
end
end
33 changes: 0 additions & 33 deletions spec/lib/form1010_ezr/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,39 +196,6 @@ def expect_personal_info_log(message)
end
end

describe '#log_exhausted_submission_failure' do
context "when 'parsed_form' is not present" do
it 'only increments StatsD' do
allow(StatsD).to receive(:increment)
expect(StatsD).to receive(:increment).with('api.1010ezr.failed_wont_retry')

described_class.new(nil).log_exhausted_submission_failure(nil)
end
end

context "when 'parsed_form' is present" do
it "increments StatsD, creates a 'PersonalInformationLog' record, and logs a failure message to sentry" do
allow(StatsD).to receive(:increment)

expect(StatsD).to receive(:increment).with('api.1010ezr.failed_wont_retry')
expect_any_instance_of(SentryLogging).to receive(:log_message_to_sentry).with(
'1010EZR total failure',
:error,
{
first_initial: 'F',
middle_initial: 'M',
last_initial: 'Z'
},
ezr: :total_failure
)

described_class.new(nil).log_exhausted_submission_failure(form)

expect_personal_info_log('Form1010Ezr FailedWontRetry')
end
end
end

describe '#submit_form' do
it 'submits the ezr with a background job', run_at: 'Tue, 21 Nov 2023 20:42:44 GMT' do
VCR.use_cassette(
Expand Down
55 changes: 34 additions & 21 deletions spec/sidekiq/hca/ezr_submission_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,43 @@
let(:ezr_service) { double }

describe 'when retries are exhausted' do
let(:msg) do
{
'args' => [encrypted_form, nil]
}
context 'when the parsed form is not present' do
it 'only increments StatsD' do
msg = {
'args' => [HealthCareApplication::LOCKBOX.encrypt({}.to_json), nil]
}

described_class.within_sidekiq_retries_exhausted_block(msg) do
allow(StatsD).to receive(:increment)
expect(StatsD).to receive(:increment).with('api.1010ezr.failed_wont_retry')
end
end
end

it "increments StatsD, creates a 'PersonalInformationLog' record, and logs a failure message to sentry" do
described_class.within_sidekiq_retries_exhausted_block(msg) do
expect(StatsD).to receive(:increment).with('api.1010ezr.failed_wont_retry')
expect_any_instance_of(SentryLogging).to receive(:log_message_to_sentry).with(
'1010EZR total failure',
:error,
{
first_initial: 'F',
middle_initial: 'M',
last_initial: 'Z'
},
ezr: :total_failure
)
end
context 'when the parsed form is present' do
it "increments StatsD, creates a 'PersonalInformationLog' record, and logs a failure message to sentry" do
msg = {
'args' => [encrypted_form, nil]
}

described_class.within_sidekiq_retries_exhausted_block(msg) do
expect(StatsD).to receive(:increment).with('api.1010ezr.failed_wont_retry')
expect_any_instance_of(SentryLogging).to receive(:log_message_to_sentry).with(
'1010EZR total failure',
:error,
{
first_initial: 'F',
middle_initial: 'M',
last_initial: 'Z'
},
ezr: :total_failure
)
end

pii_log = PersonalInformationLog.last
expect(pii_log.error_class).to eq('Form1010Ezr FailedWontRetry')
expect(pii_log.data).to eq(form)
pii_log = PersonalInformationLog.last
expect(pii_log.error_class).to eq('Form1010Ezr FailedWontRetry')
expect(pii_log.data).to eq(form)
end
end
end

Expand Down

0 comments on commit 7652af6

Please sign in to comment.