Skip to content

Commit

Permalink
more code consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
pennja committed Sep 6, 2024
1 parent 6b1598a commit f06fd16
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,16 @@ def initialize(form_id: nil, submission_id: nil, submission: nil, **options)
@failures = []
@form_id = form_id
@submission = submission || FormSubmission.find(submission_id)
@parent_dir = defaults[:parent_dir]
@include_text_archive = defaults[:include_text_archive]
@include_json_archive = defaults[:include_json_archive]
@quiet_upload_failures = defaults[:quiet_upload_failures]
@quiet_pdf_failures = defaults[:quiet_pdf_failures]
@run_quiet = defaults[:run_quiet]

assign_instance_variables(defaults)
end

def run
log_info("Processing submission ID: #{submission.id}")
process_submission_files
output_directory_path
rescue => e
handle_run_error(e)
handle_error("Failed submission: #{submission.id}", e, submission_id: submission.id)
end

private
Expand All @@ -62,7 +58,7 @@ def default_options
parent_dir: 'wipn8923-test',
quiet_pdf_failures: true, # skip PDF generation silently
quiet_upload_failures: true, # skip problematic uploads silently
run_quiet: true
run_quiet: true # silence but record errors, logged at the end
}
end

Expand All @@ -74,13 +70,6 @@ def process_submission_files
write_metadata
end

def handle_run_error(error)
raise error unless run_quiet

failures << { id: submission.id, error: error.message }
log_error("Failed submission: #{submission.id}", error)
end

def write_pdf
encoded_pdf = generate_pdf_content
save_file_to_s3("#{output_directory_path}/form.pdf", Base64.decode64(encoded_pdf))
Expand Down Expand Up @@ -124,7 +113,10 @@ def write_user_uploads
end

def process_user_upload(upload)
log_info("Processing upload: #{upload['name']} - #{upload['confirmationCode']}")
log_info(
"Processing upload: #{upload['name']} - #{upload['confirmationCode']}",
{ name: upload['name'], confirmation_code: upload['confirmationCode'] }
)
# TODO: update this logic in preference of a configurable attachment type
local_file = SupportingEvidenceAttachment.find_by(guid: upload['confirmationCode'])
raise 'Local record not found' unless local_file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ def initialize(submission_ids:, **options)
defaults = default_options.merge(options)

@submission_ids = submission_ids
@parent_dir = defaults[:parent_dir]
@bundle_by_user = defaults[:bundle_by_user]
@run_quiet = defaults[:run_quiet]
@quiet_upload_failures = defaults[:quiet_upload_failures]
@quiet_pdf_failures = defaults[:quiet_pdf_failures]
@failures = []

assign_instance_variables(defaults)
end

def run
Expand All @@ -30,8 +27,8 @@ def default_options
{
bundle_by_user: true,
parent_dir: 'wipn8923-test',
quiet_pdf_failures: false, # granular control over how user processing raises errors
quiet_upload_failures: false, # granular control over how user processing raises errors
quiet_pdf_failures: false, # granular control over how pdf processing raises errors
quiet_upload_failures: false, # granular control over how upload processing raises errors
run_quiet: true # silence but record errors until the end
}
end
Expand Down Expand Up @@ -70,7 +67,7 @@ def process_individual_submissions
def process_user_submissions(uuid, submission_ids)
UserSubmissionArchiveHandler.new(uuid:, submission_ids:, parent_dir:).run
rescue => e
handle_error("User failure: #{uuid}", e, uuid:)
handle_error("User submission archiver failure: #{uuid}", e, uuid:)
end

def process_submission(submission_id)
Expand All @@ -81,14 +78,7 @@ def process_submission(submission_id)
quiet_upload_failures:
).run
rescue => e
handle_error("Submission failure: #{submission_id}", e, submission_id:)
end

def handle_error(message, error, context)
raise unless run_quiet

log_error(message, error, context)
failures << { context => error }
handle_error("Submission archiver failure: #{submission_id}", e, submission_id:)
end

def cleanup_tmp_files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module S3Service
class Utils
private

def assign_instance_variables(defaults)
defaults.each do |key, value|
instance_variable_set("@#{key}", value)
end
end

def log_info(message, **details)
Rails.logger.info(message, details)
end
Expand All @@ -13,6 +19,13 @@ def log_error(message, error, **details)
Rails.logger.error(message, details.merge(error: error.message, backtrace: error.backtrace.first(5)))
end

def handle_error(message, error, context)
raise error unless run_quiet

log_error(message, error, context)
failures << { message:, error:, **context }
end

def s3_resource
@s3_resource ||= Reports::Uploader.new_s3_resource
end
Expand Down

0 comments on commit f06fd16

Please sign in to comment.