Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne-weibel committed Sep 6, 2024
1 parent 9850890 commit 4b38b43
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/models/form_submission_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class FormSubmissionAttempt < ApplicationRecord
transitions from: :pending, to: :vbms
transitions from: :success, to: :vbms
end

event :remediate do
transitions from: :failure, to: :vbms
end
end

def log_status_change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,68 @@

require 'lighthouse/benefits_intake/service'

class BenefitsIntakeRemediationJob
class BenefitsIntakeRemediationStatusJob
include Sidekiq::Job

sidekiq_options retry: false

STATS_KEY = 'api.benefits_intake.remediation_status'
BATCH_SIZE = Settings.lighthouse.benefits_intake.report.batch_size || 1000

attr_reader :batch_size

def initialize(batch_size: BATCH_SIZE)
@batch_size = batch_size
@total_handled = 0
end

def perform
Rails.logger.info('BenefitsIntakeRemediationJob started')
Rails.logger.info('BenefitsIntakeRemediationStatusJob started')

form_submissions = FormSubmission.includes(:form_submission_attempts)
fs_saved_claim_ids = form_submissions.map(&:saved_claim_id).uniq

started = form_submissions.group(:form_type).minimum(:created_at)
started.each do |form_id, created_at|
claim_ids = SavedClaim.where(form_id:).where("created_at > ?", created_at:).map(&:id).uniq

Check failure on line 26 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.


Check failure on line 28 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/EmptyLines: Extra blank line detected.
puts "UNSUBMITTED CLAIMS: #{claim_ids - fs_saved_claim_ids}"

Check failure on line 29 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Rails/Output: Do not write to stdout. Use Rails's logger if you want to log.
puts "ORPHAN SUBMISSION: #{fs_saved_claim_ids - claim_ids}"

Check failure on line 30 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Rails/Output: Do not write to stdout. Use Rails's logger if you want to log.
end

start_dates = FormSubmission.group(:form_type).minimum(:created_at)
failed = FormSubmission.joins(:form_submission_attempts).where(form_type: '21P-527EZ', form_submission_attempts: {aasm_state: 'failure'})
failed = form_submissions.where(form_submission_attempts: {aasm_state: 'failure'})

Check failure on line 33 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `failed`.

Check failure on line 33 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/SpaceInsideHashLiteralBraces: Space inside { missing.

Check failure on line 33 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/SpaceInsideHashLiteralBraces: Space inside } missing.

pending_form_submissions = FormSubmission
.joins(:form_submission_attempts)
.where(form_submission_attempts: { aasm_state: 'pending' })
total_handled, result = batch_process(pending_form_submissions)
batch_process(submissions)

Rails.logger.info('BenefitsIntakeRemediationJob ended', total_handled:) if result
Rails.logger.info('BenefitsIntakeRemediationStatusJob ended', total_handled:)
end

private

def batch_process(pending_form_submissions)
total_handled = 0
attr_reader :batch_size
attr_accessor :total_handled

def batch_process(submissions)
intake_service = BenefitsIntake::Service.new

pending_form_submissions.each_slice(batch_size) do |batch|
submissions.each_slice(batch_size) do |batch|
batch_uuids = batch.map(&:benefits_intake_uuid)
response = intake_service.bulk_status(uuids: batch_uuids)
raise response.body unless response.success?

total_handled += handle_response(response, batch)
next unless data = response.body['data']

Check failure on line 53 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/AssignmentInCondition: Use `==` if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.

handle_response(data, batch)
end

Check failure on line 57 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/EmptyLinesAroundExceptionHandlingKeywords: Extra empty line detected before the `rescue`.
[total_handled, true]
rescue => e
Rails.logger.error('Error processing Intake Status batch', class: self.class.name, message: e.message)
[total_handled, false]
end

# rubocop:disable Metrics/MethodLength
def handle_response(response, pending_form_submissions)
total_handled = 0

response.body['data']&.each do |submission|
def handle_response(response_data, form_submissions)
response_data.each do |submission|
uuid = submission['id']
form_submission = pending_form_submissions.find do |submission_from_db|
form_submission = form_submissions.find do |submission_from_db|
submission_from_db.benefits_intake_uuid == uuid
end
form_id = form_submission.form_type
Expand Down Expand Up @@ -85,14 +91,12 @@ def handle_response(response, pending_form_submissions)

total_handled += 1

Check failure on line 92 in app/sidekiq/benefits_intake_remediation_status_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Lint/UselessAssignment: Useless assignment to variable - `total_handled`. Use `+` instead of `+=`.
end

total_handled
end
# rubocop:enable Metrics/MethodLength

def log_result(result, form_id, uuid, time_to_transition = nil)
StatsD.increment("#{STATS_KEY}.#{form_id}.#{result}")
StatsD.increment("#{STATS_KEY}.all_forms.#{result}")
Rails.logger.info('BenefitsIntakeStatusJob', result:, form_id:, uuid:, time_to_transition:)
Rails.logger.info('BenefitsIntakeRemediationStatusJob', result:, form_id:, uuid:, time_to_transition:)
end
end
4 changes: 3 additions & 1 deletion lib/periodic_jobs.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

# @see https://crontab.guru/
# @see https://en.wikipedia.org/wiki/Cron
PERIODIC_JOBS = lambda { |mgr|
mgr.tz = ActiveSupport::TimeZone.new('America/New_York')

Expand Down Expand Up @@ -53,7 +55,7 @@
mgr.register('0 0 * * *', 'BenefitsIntakeStatusJob')

# Generate FormSubmissionAttempt rememdiation statistics from Lighthouse Benefits Intake API
mgr.register('0 0 * * *', 'BenefitsIntakeRemediationJob')
mgr.register('0 1 * * 1', 'BenefitsIntakeRemediationJob')

# Update Lighthouse526DocumentUpload statuses according to Lighthouse Benefits Documents service tracking
mgr.register('15 * * * *', 'Form526DocumentUploadPollingJob')
Expand Down

0 comments on commit 4b38b43

Please sign in to comment.