-
Notifications
You must be signed in to change notification settings - Fork 62
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
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5aad486
Create LighthouseDocumentUpload table
NB28VT d774425
Add state machine to LighthouseDocumentUpload
NB28VT 87b4214
Add event transition logging to LighthouseDocumentUpload
NB28VT e9039f0
Create class for parsing Lighthouse Document Status response
NB28VT 9f944d6
WORKING COMMIT AMEND
NB28VT de4558d
Implement UploadStatusUpdater
NB28VT 90752a5
Working version but switching to another branch
NB28VT d079b55
Create Lighthouse526DocumentUploads table
NB28VT eb3eb55
Overhaul and simplify document model
NB28VT 108091b
Refactor Status Updater
NB28VT 4d6e21d
Refactor UpdateDocumentsStatusService for new logging approach
NB28VT 7f922e6
Implement Lighthouse::Form526DocumentUploadPollingJob
NB28VT 7baac10
Refactor UpdateDocumentsStatusService for new flow
NB28VT aaa88e5
Freeze development at good stopping place for feedback
NB28VT a2476c1
Cleanup for draft review
NB28VT c0dac15
Complete e2e testing and debugging
NB28VT b4d118b
Handle failure responses from status endpoint
NB28VT 0bce3ee
hopefully cleaning up the conflicts
tblackwe 9d1b157
oops
tblackwe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
app/sidekiq/lighthouse/form_526_document_upload_polling_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
18 changes: 18 additions & 0 deletions
18
db/migrate/20240304203337_create_lighthouse526_document_uploads.rb
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to new PR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.