Skip to content

Commit

Permalink
initial commit of pcpg work based on exhisting benefits intake zsf wo…
Browse files Browse the repository at this point in the history
…rk (#19180)

* initial commit of pcpg work based on exhisting benefits intake zsf work

* rubocop updates

* update codeowners

* add emails and tests

* put back retries

* remove zsf from benefits intake

* refactor

* add test for claim being nil
  • Loading branch information
evansmith authored Nov 4, 2024
1 parent deb41e2 commit be1f199
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ lib/mpi @department-of-veterans-affairs/octo-identity
lib/okta/directory_service.rb @department-of-veterans-affairs/lighthouse-pivot
lib/olive_branch_patch.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
lib/pagerduty @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
lib/pcpg @department-of-veterans-affairs/benefits-non-disability @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
lib/pdf_fill @department-of-veterans-affairs/vsa-debt-resolution @department-of-veterans-affairs/backend-review-group @department-of-veterans-affairs/vfs-10-10 @department-of-veterans-affairs/vfs-1095-b
lib/pdf_fill/forms/pdfs/21P-0969.pdf @department-of-veterans-affairs/pension-and-burials @department-of-veterans-affairs/backend-review-group
lib/pdf_fill/forms/va21p0969.rb @department-of-veterans-affairs/pension-and-burials @department-of-veterans-affairs/backend-review-group
Expand Down Expand Up @@ -1464,6 +1465,7 @@ spec/lib/okta @department-of-veterans-affairs/lighthouse-pivot
spec/lib/okta/directory_service_spec.rb @department-of-veterans-affairs/lighthouse-pivot
spec/lib/olive_branch_patch_spec.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/lib/pagerduty @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/lib/pcpg @department-of-veterans-affairs/benefits-non-disability @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/lib/pdf_fill @department-of-veterans-affairs/backend-review-group
spec/lib/pdf_info/metadata_spec.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
spec/lib/pdf_utilities @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
Expand Down
1 change: 1 addition & 0 deletions app/sidekiq/lighthouse/submit_benefits_intake_claim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'pdf_utilities/datestamp_pdf'
require 'pension_burial/tag_sentry'
require 'burials/monitor'
require 'pcpg/monitor'
require 'benefits_intake_service/service'
require 'simple_forms_api_submission/metadata_validator'
require 'pdf_info'
Expand Down
31 changes: 27 additions & 4 deletions app/sidekiq/lighthouse/submit_career_counseling_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'pcpg/monitor'

module Lighthouse
class SubmitCareerCounselingJob
include Sidekiq::Job
Expand All @@ -10,10 +12,16 @@ class SubmitCareerCounselingJob
sidekiq_options retry: RETRY

sidekiq_retries_exhausted do |msg, _ex|
Rails.logger.error(
"Failed all retries on SubmitCareerCounselingJob, last error: #{msg['error_message']}"
)
StatsD.increment("#{STATSD_KEY_PREFIX}.exhausted")
begin
claim = SavedClaim.find(msg['args'].first)
rescue
claim = nil
end

pcpg_monitor = PCPG::Monitor.new
pcpg_monitor.track_submission_exhaustion(msg, claim)

Lighthouse::SubmitCareerCounselingJob.trigger_failure_events(claim)
end

def perform(claim_id, user_uuid = nil)
Expand Down Expand Up @@ -49,5 +57,20 @@ def send_confirmation_email(user_uuid)
}
)
end

def self.trigger_failure_events(claim)
email = claim.parsed_form.dig('claimantInformation', 'emailAddress')
if claim.present? && email.present?
VANotify::EmailJob.perform_async(
email,
Settings.vanotify.services.va_gov.template_id.form27_8832_action_needed_email,
{
'first_name' => claim.parsed_form.dig('claimantInformation', 'fullName', 'first')&.upcase.presence,
'date' => Time.zone.today.strftime('%B %d, %Y'),
'confirmation_number' => claim.confirmation_number
}
)
end
end
end
end
3 changes: 3 additions & 0 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,9 @@ features:
organic_conversion_experiment:
actor_type: user
description: Toggle to enable login.gov create account experiment
pcpg_trigger_action_needed_email:
actor_type: user
description: Set whether to enable VANotify email to Veteran for PCPG failure exhaustion
pension_income_and_assets_clarification:
actor_type: user
description: >
Expand Down
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@ vanotify:
form21_4142_confirmation_email: form21_4142_confirmation_email_template_id
form21_4142_error_email: form21_4142_error_email_template_id
form21_4142_received_email: form21_4142_received_email_template_id
form27_8832_action_needed_email: form27_8832_action_needed_email_template_id
form40_0247_confirmation_email: form40_0247_confirmation_email_template_id
form40_0247_error_email: form40_0247_error_email_template_id
form40_10007_error_email: form40_10007_error_email_template_id
Expand Down
60 changes: 60 additions & 0 deletions lib/pcpg/monitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'zero_silent_failures/monitor'

module PCPG
##
# Monitor functions for Rails logging and StatsD
#
class Monitor < ::ZeroSilentFailures::Monitor
# statsd key for api
CLAIM_STATS_KEY = 'career-guidance-application'

# statsd key for submit career counseling sidekiq
SUBMISSION_STATS_KEY = 'worker.lighthouse.submit_career_counseling_job'
# statsd key for benefits intake sidekiq
BENEFITS_INTAKE_SUBMISSION_STATS_KEY = 'worker.lighthouse.submit_benefits_intake_claim'

def initialize
super('career-guidance-application')
end

def track_submission_exhaustion(msg, claim = nil)
user_account_uuid = msg['args'].length <= 1 ? nil : msg['args'][1]
additional_context = {
form_id: claim&.form_id,
claim_id: msg['args'].first,
confirmation_number: claim&.confirmation_number,
message: msg
}
# log_silent_failure calls the ZSF method which increases a special StatsD metric
# and writes to the Rails log for additional ZSF tracking.
log_silent_failure(additional_context, user_account_uuid, call_location: caller_locations.first)

StatsD.increment("#{SUBMISSION_STATS_KEY}.exhausted")
Rails.logger.error(
"Failed all retries on SubmitCareerCounselingJob, last error: #{msg['error_message']}",
user_uuid: user_account_uuid, **additional_context
)
end

def track_benefits_intake_submission_exhaustion(msg, claim = nil)
user_account_uuid = msg['args'].length <= 1 ? nil : msg['args'][1]
additional_context = {
form_id: claim&.form_id,
claim_id: msg['args'].first,
confirmation_number: claim&.confirmation_number,
message: msg
}
# log_silent_failure calls the ZSF method which increases a special StatsD metric
# and writes to the Rails log for additional ZSF tracking.
log_silent_failure(additional_context, user_account_uuid, call_location: caller_locations.first)

StatsD.increment("#{BENEFITS_INTAKE_SUBMISSION_STATS_KEY}.exhausted")
Rails.logger.error(
'Lighthouse::SubmitBenefitsIntakeClaim PCPG 28-8832 submission to LH exhausted!',
user_uuid: user_account_uuid, **additional_context
)
end
end
end
77 changes: 77 additions & 0 deletions spec/lib/pcpg/monitor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require 'rails_helper'
require_relative '../../../lib/pcpg/monitor'

RSpec.describe PCPG::Monitor do
let(:monitor) { described_class.new }
let(:claim_stats_key) { described_class::CLAIM_STATS_KEY }
let(:submission_stats_key) { described_class::SUBMISSION_STATS_KEY }
let(:benefits_intake_submission_stats_key) { described_class::BENEFITS_INTAKE_SUBMISSION_STATS_KEY }
let(:claim) { create(:education_career_counseling_claim) }
let(:ipf) { create(:in_progress_form) }

context 'with all params supplied' do
let(:current_user) { create(:user) }
let(:monitor_error) { create(:monitor_error) }
let(:lh_service) { OpenStruct.new(uuid: 'uuid') }

describe '#track_submission_exhaustion' do
it 'logs sidekiq job exhaustion' do
msg = { 'args' => [claim.id, current_user.uuid], error_message: 'Error!' }

log = "Failed all retries on SubmitCareerCounselingJob, last error: #{msg['error_message']}"
payload = {
form_id: claim.form_id,
claim_id: claim.id,
confirmation_number: claim.confirmation_number,
message: msg
}

expect(monitor).to receive(:log_silent_failure).with(payload, current_user.uuid, anything)
expect(StatsD).to receive(:increment).with("#{submission_stats_key}.exhausted")
expect(Rails.logger).to receive(:error).with(log, user_uuid: current_user.uuid, **payload)

monitor.track_submission_exhaustion(msg, claim)
end

it 'logs with no claim information if claim is passed in as nil' do
msg = { 'args' => [claim.id, current_user.uuid], error_message: 'Error!' }

log = "Failed all retries on SubmitCareerCounselingJob, last error: #{msg['error_message']}"
payload = {
form_id: nil,
claim_id: msg['args'].first,
confirmation_number: nil,
message: msg
}

expect(monitor).to receive(:log_silent_failure).with(payload, current_user.uuid, anything)
expect(StatsD).to receive(:increment).with("#{submission_stats_key}.exhausted")
expect(Rails.logger).to receive(:error).with(log, user_uuid: current_user.uuid, **payload)

monitor.track_submission_exhaustion(msg, nil)
end
end

describe '#track_benefits_intake_submission_exhaustion' do
it 'logs sidekiq job exhaustion' do
msg = { 'args' => [claim.id, current_user.uuid] }

log = 'Lighthouse::SubmitBenefitsIntakeClaim PCPG 28-8832 submission to LH exhausted!'
payload = {
form_id: claim.form_id,
claim_id: claim.id,
confirmation_number: claim.confirmation_number,
message: msg
}

expect(monitor).to receive(:log_silent_failure).with(payload, current_user.uuid, anything)
expect(StatsD).to receive(:increment).with("#{benefits_intake_submission_stats_key}.exhausted")
expect(Rails.logger).to receive(:error).with(log, user_uuid: current_user.uuid, **payload)

monitor.track_benefits_intake_submission_exhaustion(msg, claim)
end
end
end
end
51 changes: 46 additions & 5 deletions spec/sidekiq/lighthouse/submit_career_counseling_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# frozen_string_literal: true

require 'rails_helper'
require 'pcpg/monitor'

RSpec.describe Lighthouse::SubmitCareerCounselingJob do
let(:claim) { create(:education_career_counseling_claim) }
let(:job) { described_class.new }
let(:monitor) { double('monitor') }
let(:exhaustion_msg) do
{ 'args' => [], 'class' => 'Lighthouse::SubmitCareerCounselingJob', 'error_message' => 'An error occured',
'queue' => nil }
end
let(:user_account_uuid) { 123 }

describe '#perform' do
it 'sends to central mail' do
Expand Down Expand Up @@ -58,13 +65,47 @@
end
end

describe 'sidekiq_retries_exhausted block' do
describe 'sidekiq_retries_exhausted block with flipper on' do
before do
Flipper.enable(:form27_8832_action_needed_email)
allow(PCPG::Monitor).to receive(:new).and_return(monitor)
allow(monitor).to receive :track_submission_exhaustion
end

it 'logs error when retries are exhausted' do
Lighthouse::SubmitCareerCounselingJob.within_sidekiq_retries_exhausted_block do
expect(Rails.logger).to receive(:error).exactly(:once).with(
'Failed all retries on SubmitCareerCounselingJob, last error: An error occured'
Lighthouse::SubmitCareerCounselingJob.within_sidekiq_retries_exhausted_block(
{ 'args' => [claim.id, user_account_uuid] }
) do
expect(SavedClaim).to receive(:find).with(claim.id).and_return(claim)
exhaustion_msg['args'] = [claim.id, user_account_uuid]
expect(monitor).to receive(:track_submission_exhaustion).with(exhaustion_msg, claim)
expect(VANotify::EmailJob).to receive(:perform_async).with(
'foo@foo.com',
'form27_8832_action_needed_email_template_id',
{
'first_name' => 'DERRICK',
'date' => Time.now.in_time_zone('Eastern Time (US & Canada)').strftime('%B %d, %Y'),
'confirmation_number' => claim.confirmation_number
}
)
expect(StatsD).to receive(:increment).with('worker.lighthouse.submit_career_counseling_job.exhausted')
end
end
end

describe 'sidekiq_retries_exhausted block with flipper off' do
before do
Flipper.disable(:form27_8832_action_needed_email)
allow(PCPG::Monitor).to receive(:new).and_return(monitor)
allow(monitor).to receive :track_submission_exhaustion
end

it 'logs error when retries are exhausted' do
Lighthouse::SubmitCareerCounselingJob.within_sidekiq_retries_exhausted_block(
{ 'args' => [claim.id, user_account_uuid] }
) do
expect(SavedClaim).to receive(:find).with(claim.id).and_return(claim)
exhaustion_msg['args'] = [claim.id, user_account_uuid]
expect(monitor).to receive(:track_submission_exhaustion).with(exhaustion_msg, claim)
end
end
end
Expand Down

0 comments on commit be1f199

Please sign in to comment.