Skip to content

Commit

Permalink
API-35187: Remove performance benchmark code from Benefits Intake Upl…
Browse files Browse the repository at this point in the history
…oadProcessor (#16147)
  • Loading branch information
kristen-brown authored Apr 1, 2024
1 parent 979e6cd commit f22d32c
Showing 1 changed file with 31 additions and 59 deletions.
90 changes: 31 additions & 59 deletions modules/vba_documents/app/sidekiq/vba_documents/upload_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,27 @@ class UploadProcessor
include VBADocuments::UploadValidations

STATSD_DUPLICATE_UUID_KEY = 'api.vba.document_upload.duplicate_uuid'
STATSD_TIMING = 'api.vba.document_upload_perf_timing'

# Ensure that multiple jobs for the same GUID aren't spawned,
# to avoid race condition when parsing the multipart file
sidekiq_options unique_for: 30.days

def perform(guid, caller_data, retries = 0)
# @retries variable used via the CentralMail::Utilities which is included via VBADocuments::UploadValidations
@retries = retries
@cause = caller_data.nil? ? { caller: 'unknown' } : caller_data['caller']
response = nil
brt = Benchmark.realtime do
# @retries variable used via the CentralMail::Utilities which is included via VBADocuments::UploadValidations
@retries = retries
@cause = caller_data.nil? ? { caller: 'unknown' } : caller_data['caller']
response = nil
VBADocuments::UploadSubmission.with_advisory_lock(guid) do
@upload = VBADocuments::UploadSubmission.where(status: 'uploaded').find_by(guid:)
if @upload
tracking_hash = { 'job' => 'VBADocuments::UploadProcessor' }.merge(@upload.as_json)
Rails.logger.info('VBADocuments: Start Processing.', tracking_hash)
response = download_and_process
tracking_hash = { 'job' => 'VBADocuments::UploadProcessor' }.merge(@upload.reload.as_json)
Rails.logger.info('VBADocuments: Stop Processing.', tracking_hash)
end

VBADocuments::UploadSubmission.with_advisory_lock(guid) do
@upload = VBADocuments::UploadSubmission.where(status: 'uploaded').find_by(guid:)
if @upload
tracking_hash = { 'job' => 'VBADocuments::UploadProcessor' }.merge(@upload.as_json)
Rails.logger.info('VBADocuments: Start Processing.', tracking_hash)
response = download_and_process
tracking_hash = { 'job' => 'VBADocuments::UploadProcessor' }.merge(@upload.reload.as_json)
Rails.logger.info('VBADocuments: Stop Processing.', tracking_hash)
end
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload&.guid}", 'step: perform_complete',
"time: #{brt.round(5)}}"])

response&.success? ? true : false
end
Expand All @@ -48,54 +43,31 @@ def perform(guid, caller_data, retries = 0)

# rubocop:disable Metrics/MethodLength
def download_and_process
tempfile, timestamp = nil
brt = Benchmark.realtime do
tempfile, timestamp = VBADocuments::PayloadManager.download_raw_file(@upload.guid)
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload.guid}", 'step: download_raw_file',
"raw_file_size: #{tempfile.size}", "time: #{brt.round(5)}}"])

tempfile, timestamp = VBADocuments::PayloadManager.download_raw_file(@upload.guid)
response = nil

begin
parts, inspector = nil
brt = Benchmark.realtime do
@upload.update(metadata: @upload.metadata.merge(original_file_metadata(tempfile)))
@upload.update(metadata: @upload.metadata.merge(original_file_metadata(tempfile)))

validate_payload_size(tempfile)
validate_payload_size(tempfile)

parts = VBADocuments::MultipartParser.parse(tempfile.path)
inspector = VBADocuments::PDFInspector.new(pdf: parts)
@upload.update(uploaded_pdf: inspector.pdf_data)
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload.guid}", 'step: parse_parts',
"time: #{brt.round(5)}}"])

metadata = nil
brt = Benchmark.realtime do
# Validations
validate_parts(@upload, parts)
validate_metadata(parts[META_PART_NAME], @upload.consumer_id, @upload.guid,
submission_version: @upload.metadata['version'].to_i)
metadata = perfect_metadata(@upload, parts, timestamp)

pdf_validator_options = VBADocuments::DocumentRequestValidator.pdf_validator_options
validate_documents(parts, pdf_validator_options)
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload.guid}", 'step: validate',
"time: #{brt.round(5)}}"])
parts = VBADocuments::MultipartParser.parse(tempfile.path)
inspector = VBADocuments::PDFInspector.new(pdf: parts)
@upload.update(uploaded_pdf: inspector.pdf_data)

brt = Benchmark.realtime do
response = submit(metadata, parts)
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload.guid}", 'step: cm_upload',
"time: #{brt.round(5)}}"])
# Validations
validate_parts(@upload, parts)
validate_metadata(parts[META_PART_NAME], @upload.consumer_id, @upload.guid,
submission_version: @upload.metadata['version'].to_i)
metadata = perfect_metadata(@upload, parts, timestamp)

brt = Benchmark.realtime do
process_response(response)
log_submission(@upload, metadata)
end
StatsD.increment(STATSD_TIMING, tags: ["jid: #{jid}", "guid: #{@upload.guid}", 'step: process_resp',
"time: #{brt.round(5)}}"])
pdf_validator_options = VBADocuments::DocumentRequestValidator.pdf_validator_options
validate_documents(parts, pdf_validator_options)

response = submit(metadata, parts)

process_response(response)
log_submission(@upload, metadata)
rescue Common::Exceptions::GatewayTimeout => e
handle_gateway_timeout(e)
rescue VBADocuments::UploadError => e
Expand Down

0 comments on commit f22d32c

Please sign in to comment.