Class: Form526StatusPollingJob

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/form526_status_polling_job.rb

Constant Summary collapse

STATS_KEY =
'api.benefits_intake.submission_status'
MAX_BATCH_SIZE =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_batch_size: MAX_BATCH_SIZE) ⇒ Form526StatusPollingJob

Returns a new instance of Form526StatusPollingJob.



13
14
15
16
# File 'app/sidekiq/form526_status_polling_job.rb', line 13

def initialize(max_batch_size: MAX_BATCH_SIZE)
  @max_batch_size = max_batch_size
  @total_handled = 0
end

Instance Attribute Details

#max_batch_sizeObject (readonly)

Returns the value of attribute max_batch_size.



11
12
13
# File 'app/sidekiq/form526_status_polling_job.rb', line 11

def max_batch_size
  @max_batch_size
end

Instance Method Details

#api_to_pollObject (private)



34
35
36
# File 'app/sidekiq/form526_status_polling_job.rb', line 34

def api_to_poll
  @api_to_poll ||= BenefitsIntakeService::Service.new
end

#handle_response(response) ⇒ Object (private)



42
43
44
45
46
47
48
49
50
# File 'app/sidekiq/form526_status_polling_job.rb', line 42

def handle_response(response)
  response.body['data']&.each do |submission|
    status = submission.dig('attributes', 'status')
    form_submission = Form526Submission.find_by(backup_submitted_claim_id: submission['id'])

    handle_submission(status, form_submission)
    @total_handled += 1
  end
end

#handle_submission(status, form_submission) ⇒ Object (private)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/sidekiq/form526_status_polling_job.rb', line 52

def handle_submission(status, form_submission)
  submission_id = form_submission.id

  if %w[error expired].include? status
    log_result('failure', submission_id)
    form_submission.rejected!
    notify_veteran(submission_id)
  elsif status == 'vbms'
    log_result('true_success', submission_id)
    form_submission.accepted!
  elsif status == 'success'
    log_result('paranoid_success', submission_id)
    form_submission.paranoid_success!
  else
    Rails.logger.info(
      'Unknown or incomplete status returned from Benefits Intake API for 526 submission',
      status:,
      submission_id:
    )
  end
end

#log_result(result, submission_id) ⇒ Object (private)



74
75
76
77
78
79
# File 'app/sidekiq/form526_status_polling_job.rb', line 74

def log_result(result, submission_id)
  StatsD.increment("#{STATS_KEY}.526.#{result}")
  StatsD.increment("#{STATS_KEY}.all_forms.#{result}")

  Rails.logger.warn('Form526StatusPollingJob submission failure', { result:, submission_id: }) if result == 'failure'
end

#notify_veteran(submission_id) ⇒ Object (private)



81
82
83
84
85
# File 'app/sidekiq/form526_status_polling_job.rb', line 81

def notify_veteran(submission_id)
  if Flipper.enabled?(:form526_send_backup_submission_polling_failure_email_notice)
    Form526SubmissionFailureEmailJob.perform_async(submission_id, Time.now.utc.to_s)
  end
end

#performObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/sidekiq/form526_status_polling_job.rb', line 18

def perform
  Rails.logger.info('Beginning Form 526 Intake Status polling')
  submissions.in_batches(of: max_batch_size) do |batch|
    batch_ids = batch.pluck(:backup_submitted_claim_id).flatten
    response = api_to_poll.get_bulk_status_of_uploads(batch_ids)
    handle_response(response)
  end
  Rails.logger.info('Form 526 Intake Status polling complete',
                    total_handled: @total_handled)
rescue => e
  Rails.logger.error('Error processing 526 Intake Status batch',
                     class: self.class.name, message: e.message)
end

#submissionsObject (private)



38
39
40
# File 'app/sidekiq/form526_status_polling_job.rb', line 38

def submissions
  @submissions ||= Form526Submission.pending_backup
end