Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form 526 Document Upload - Lighthouse status polling system #15964

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/models/lighthouse526_document_upload.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to second PR, create and test the Model.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

class Lighthouse526DocumentUpload < ApplicationRecord
include AASM

VETERAN_UPLOAD_DOCUMENT_TYPE = 'Veteran Upload'
BDD_INSTRUCTIONS_DOCUMENT_TYPE = 'BDD Instructions'
FORM_0781_DOCUMENT_TYPE = 'Form 0781'
FORM_0781A_DOCUMENT_TYPE = 'Form 0781a'

VALID_DOCUMENT_TYPES = [
BDD_INSTRUCTIONS_DOCUMENT_TYPE,
FORM_0781_DOCUMENT_TYPE,
FORM_0781A_DOCUMENT_TYPE,
VETERAN_UPLOAD_DOCUMENT_TYPE
].freeze

belongs_to :form526_submission
belongs_to :form_attachment, optional: true

validates :lighthouse_document_request_id, presence: true
validates :document_type, presence: true, inclusion: { in: VALID_DOCUMENT_TYPES }

# Veteran Uploads must reference a FormAttachment record as that is where we store a Veteran-submitted file's
# metadata, including S3 URL
validates :form_attachment, presence: true, if: :veteran_upload?

# Window for polling Lighthouse for the status of an upload
scope :status_update_required, -> { where('status_last_polled_at < ?', 1.day.ago.utc).or(where(status_last_polled_at: nil)) }

aasm do
state :pending, initial: true
state :completed, :failed

event :complete do
transitions from: :pending, to: :completed, guard: :end_time_saved?
end

event :fail do
transitions from: :pending, to: :failed, guard: %i[end_time_saved? error_message_saved?]
end
end

private

def veteran_upload?
document_type == VETERAN_UPLOAD_DOCUMENT_TYPE
end

def end_time_saved?
lighthouse_processing_ended_at != nil
end

def error_message_saved?
error_message != nil
end
end
58 changes: 58 additions & 0 deletions app/sidekiq/lighthouse/form_526_document_upload_polling_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'lighthouse/benefits_documents/form526/documents_status_polling_service'
require 'lighthouse/benefits_documents/form526/update_documents_status_service'

module Lighthouse
class Form526DocumentUploadPollingJob
include Sidekiq::Job
# This job runs every 24 hours
# Only retry within that window to avoid polling the same documents twice
# 13 retries = retry for ~17 hours
# See Sidekiq documentation for retry algorithm
# https://github.com/sidekiq/sidekiq/wiki/Error-Handling#automatic-job-retry
sidekiq_options retry: 13

POLLED_BATCH_DOCUMENT_COUNT = 100
STATSD_KEY_PREFIX = 'worker.lighthouse.poll_form_526_document_uploads'

sidekiq_retries_exhausted do |msg, _ex|
StatsD.increment("#{STATSD_KEY_PREFIX}.exhausted")

Rails.logger.warn(
'Lighthouse::Form526DocumentUploadPollingJob retries exhausted',
{
job_id: msg['jid'],
error_class: msg['error_class'],
error_message: msg['error_message'],
timestamp: Time.now.utc
}
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Add exception logger here, datadog wont capture this unless you log it

end

def perform
Lighthouse526DocumentUpload.pending.status_update_required.in_batches(
of: POLLED_BATCH_DOCUMENT_COUNT
) do |document_batch|
lighthouse_document_request_ids = document_batch.pluck(:lighthouse_document_request_id)
response = BenefitsDocuments::Form526::DocumentsStatusPollingService.call(lighthouse_document_request_ids)

if response.status == 200
BenefitsDocuments::Form526::UpdateDocumentsStatusService.call(document_batch, response.body)
else
StatsD.increment("#{STATSD_KEY_PREFIX}.polling_error")

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to new PR

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class CreateLighthouse526DocumentUploads < ActiveRecord::Migration[7.0]
def change
create_table :lighthouse526_document_uploads do |t|
t.references :form526_submission, foreign_key: true, null: false
t.references :form_attachment, foreign_key: true
t.string :lighthouse_document_request_id, null: false
t.string :aasm_state, index: true
t.string :document_type
t.datetime :lighthouse_processing_started_at
t.datetime :lighthouse_processing_ended_at
t.datetime :status_last_polled_at, index: true
t.jsonb :error_message
t.jsonb :last_status_response

t.timestamps
end
end
end
Loading
Loading