From 7652af664803453abf67fc9022ddc87e4e1f7e55 Mon Sep 17 00:00:00 2001 From: Joshua Drumm Date: Fri, 6 Sep 2024 18:22:36 -0400 Subject: [PATCH] Moved the total failure code into the job class --- app/sidekiq/hca/ezr_submission_job.rb | 21 ++++++-- lib/form1010_ezr/service.rb | 31 +++--------- spec/lib/form1010_ezr/service_spec.rb | 33 ------------- spec/sidekiq/hca/ezr_submission_job_spec.rb | 55 +++++++++++++-------- 4 files changed, 58 insertions(+), 82 deletions(-) diff --git a/app/sidekiq/hca/ezr_submission_job.rb b/app/sidekiq/hca/ezr_submission_job.rb index 649c2e423a1..da3d0a0b583 100644 --- a/app/sidekiq/hca/ezr_submission_job.rb +++ b/app/sidekiq/hca/ezr_submission_job.rb @@ -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) diff --git a/lib/form1010_ezr/service.rb b/lib/form1010_ezr/service.rb index 14fbe48f1f6..2f6b459aeb3 100644 --- a/lib/form1010_ezr/service.rb +++ b/lib/form1010_ezr/service.rb @@ -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 @@ -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 diff --git a/spec/lib/form1010_ezr/service_spec.rb b/spec/lib/form1010_ezr/service_spec.rb index de2d526a59d..0b36a41c372 100644 --- a/spec/lib/form1010_ezr/service_spec.rb +++ b/spec/lib/form1010_ezr/service_spec.rb @@ -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( diff --git a/spec/sidekiq/hca/ezr_submission_job_spec.rb b/spec/sidekiq/hca/ezr_submission_job_spec.rb index a65715a2a33..03b93b78dce 100644 --- a/spec/sidekiq/hca/ezr_submission_job_spec.rb +++ b/spec/sidekiq/hca/ezr_submission_job_spec.rb @@ -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