Class: Lighthouse::Form526DocumentUploadPollingJob

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

Constant Summary collapse

POLLED_BATCH_DOCUMENT_COUNT =
100
STATSD_KEY_PREFIX =
'worker.lighthouse.poll_form526_document_uploads'
STATSD_PENDING_DOCUMENTS_POLLED_KEY =
'pending_documents_polled'
STATSD_PENDING_DOCUMENTS_MARKED_SUCCESS_KEY =
'pending_documents_marked_completed'
STATSD_PENDING_DOCUMENTS_MARKED_FAILED_KEY =
'pending_documents_marked_failed'

Instance Method Summary collapse

Instance Method Details

#handle_error(response, lighthouse_document_request_ids) ⇒ Object (private)



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/sidekiq/lighthouse/form526_document_upload_polling_job.rb', line 90

def handle_error(response, lighthouse_document_request_ids)
  StatsD.increment("#{STATSD_KEY_PREFIX}.polling_error")

  Rails.logger.warn(
    'Lighthouse::Form526DocumentUploadPollingJob status endpoint error',
    {
      response_status: response.status,
      response_body: response.body,
      lighthouse_document_request_ids:,
      timestamp: Time.now.utc
    }
  )
end

#performObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/sidekiq/lighthouse/form526_document_upload_polling_job.rb', line 46

def perform
  successful_documents_before_polling = Lighthouse526DocumentUpload.completed.count
  failed_documents_before_polling = Lighthouse526DocumentUpload.failed.count

  documents_to_poll = Lighthouse526DocumentUpload.pending.status_update_required
  StatsD.gauge("#{STATSD_KEY_PREFIX}.#{STATSD_PENDING_DOCUMENTS_POLLED_KEY}", documents_to_poll.count)

  documents_to_poll.in_batches(
    of: POLLED_BATCH_DOCUMENT_COUNT
  ) do |document_batch|
    lighthouse_document_request_ids = document_batch.pluck(:lighthouse_document_request_id)

    update_document_batch(document_batch, lighthouse_document_request_ids)
  rescue Faraday::ResourceNotFound => e
    response_struct = OpenStruct.new(e.response)

    handle_error(response_struct, lighthouse_document_request_ids)
  end

  documents_marked_success = Lighthouse526DocumentUpload.completed.count - successful_documents_before_polling
  StatsD.gauge("#{STATSD_KEY_PREFIX}.#{STATSD_PENDING_DOCUMENTS_MARKED_SUCCESS_KEY}", documents_marked_success)

  documents_marked_failed = Lighthouse526DocumentUpload.failed.count - failed_documents_before_polling
  StatsD.gauge("#{STATSD_KEY_PREFIX}.#{STATSD_PENDING_DOCUMENTS_MARKED_FAILED_KEY}", documents_marked_failed)
end

#update_document_batch(document_batch, lighthouse_document_request_ids) ⇒ Object (private)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/sidekiq/lighthouse/form526_document_upload_polling_job.rb', line 74

def update_document_batch(document_batch, lighthouse_document_request_ids)
  response = BenefitsDocuments::Form526::DocumentsStatusPollingService.call(lighthouse_document_request_ids)

  if response.status == 200
    result = BenefitsDocuments::Form526::UpdateDocumentsStatusService.call(document_batch, response.body)

    if result && !result[:success]
      response_struct = OpenStruct.new(result[:response])

      handle_error(response_struct, response_struct.unknown_ids.map(&:to_s))
    end
  else
    handle_error(response, lighthouse_document_request_ids)
  end
end