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

Stuck Jobs Reporter and Fixes and S3 Buckets updates #19535

Merged
merged 13 commits into from
Sep 27, 2023
78 changes: 78 additions & 0 deletions app/jobs/bgs_share_error_fix_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# frozen_string_literal: true

class BgsShareErrorFixJob < CaseflowJob
ERROR_TEXT = "ShareError"
STUCK_JOB_REPORT_SERVICE = StuckJobReportService.new

def perform
clear_hlr_errors if hlrs_with_errors.present?
clear_rius_errors if rius_with_errors.present?
clear_bge_errors if bges_with_errors.present?
STUCK_JOB_REPORT_SERVICE.write_log_report(ERROR_TEXT)
end

def clear_rius_errors
STUCK_JOB_REPORT_SERVICE.append_record_count(rius_with_errors.count, ERROR_TEXT)
rius_with_errors.each do |riu|
epe = EndProductEstablishment.find_by(
id: riu.review_id
)
next if epe.established_at.blank?

resolve_error_on_records(riu)
STUCK_JOB_REPORT_SERVICE.append_single_record(riu.class.name, riu.id)
end
STUCK_JOB_REPORT_SERVICE.append_record_count(rius_with_errors.count, ERROR_TEXT)
end

def clear_hlr_errors
STUCK_JOB_REPORT_SERVICE.append_record_count(hlrs_with_errors.count, ERROR_TEXT)

hlrs_with_errors.each do |hlr|
epe = EndProductEstablishment.find_by(
veteran_file_number: hlr.veteran_file_number
)
next if epe.established_at.blank?

resolve_error_on_records(hlr)
STUCK_JOB_REPORT_SERVICE.append_single_record(hlr.class.name, hlr.id)
end
STUCK_JOB_REPORT_SERVICE.append_record_count(hlrs_with_errors.count, ERROR_TEXT)
end

def clear_bge_errors
STUCK_JOB_REPORT_SERVICE.append_record_count(bges_with_errors.count, ERROR_TEXT)

bges_with_errors.each do |bge|
next if bge.end_product_establishment.established_at.blank?

resolve_error_on_records(bge)
STUCK_JOB_REPORT_SERVICE.append_single_record(bge.class.name, bge.id)
end
STUCK_JOB_REPORT_SERVICE.append_record_count(bges_with_errors.count, ERROR_TEXT)
end

def hlrs_with_errors
HigherLevelReview.where("establishment_error ILIKE?", "%#{ERROR_TEXT}%")
end

def rius_with_errors
RequestIssuesUpdate.where("error ILIKE?", "%#{ERROR_TEXT}%")
end

def bges_with_errors
BoardGrantEffectuation.where("decision_sync_error ILIKE?", "%#{ERROR_TEXT}%")
end

private

# :reek:FeatureEnvy
def resolve_error_on_records(object_type)
ActiveRecord::Base.transaction do
object_type.clear_error!
rescue StandardError => error
log_error(error)
STUCK_JOB_REPORT_SERVICE.append_errors(object_type.class.name, object_type.id, error)
end
end
end
26 changes: 6 additions & 20 deletions app/jobs/cannot_delete_contention_remediation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
class CannotDeleteContentionRemediationJob < CaseflowJob
queue_with_priority :low_priority

# Sub folder name
S3_FOLDER_NAME = "data-remediation-output"

def initialize
@logs = ["\nVBMS::CannotDeleteContention Remediation Log"]
@remediated_request_issues_update_ids = []
@folder_name = (Rails.deploy_env == :prod) ? S3_FOLDER_NAME : "#{S3_FOLDER_NAME}-#{Rails.deploy_env}"
super
end

Expand Down Expand Up @@ -166,28 +170,10 @@ def sync_epe!(request_issues_update, request_issue, index)
" Resetting EPE synced_status to null. Syncing Epe with EP.")
end

# Save Logs to S3 Bucket
def store_logs_in_s3_bucket
# Set Client Resources for AWS
Aws.config.update(region: "us-gov-west-1")
s3client = Aws::S3::Client.new
s3resource = Aws::S3::Resource.new(client: s3client)
s3bucket = s3resource.bucket("data-remediation-output")

# Folder and File name
file_name = "cannot-delete-contention-remediation-logs/cdc-remediation-log-#{Time.zone.now}"

# Store contents of logs array in a temporary file
content = @logs.join("\n")
temporary_file = Tempfile.new("cdc-log.txt")
filepath = temporary_file.path
temporary_file.write(content)
temporary_file.flush

# Store File in S3 bucket
s3bucket.object(file_name).upload_file(filepath, acl: "private", server_side_encryption: "AES256")

# Delete Temporary File
temporary_file.close!
file_name = "cannot-delete-contention-remediation-logs/cdc-remediation-log-#{Time.zone.now}"
S3Service.store_file("#{@folder_name}/#{file_name}", content)
end
end
50 changes: 50 additions & 0 deletions app/jobs/claim_date_dt_fix_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

class ClaimDateDtFixJob < CaseflowJob
ERROR_TEXT = "ClaimDateDt"

attr_reader :stuck_job_report_service

def initialize
@stuck_job_report_service = StuckJobReportService.new
end

def perform
process_decision_documents
end

def process_decision_documents
return if decision_docs_with_errors.blank?

stuck_job_report_service.append_record_count(decision_docs_with_errors.count, ERROR_TEXT)

decision_docs_with_errors.each do |single_decision_document|
next unless valid_decision_document?(single_decision_document)

process_decision_document(single_decision_document)
end

stuck_job_report_service.append_record_count(decision_docs_with_errors.count, ERROR_TEXT)

stuck_job_report_service.write_log_report(ERROR_TEXT)
end

def valid_decision_document?(decision_document)
decision_document.processed_at.present? &&
decision_document.uploaded_to_vbms_at.present?
end

# :reek:FeatureEnvy
def process_decision_document(decision_document)
ActiveRecord::Base.transaction do
decision_document.clear_error!
rescue StandardError => error
log_error(error)
stuck_job_report_service.append_errors(decision_document.class.name, decision_document.id, error)
end
end

def decision_docs_with_errors
DecisionDocument.where("error ILIKE ?", "%#{ERROR_TEXT}%")
end
end
54 changes: 54 additions & 0 deletions app/jobs/claim_not_established_fix_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

class ClaimNotEstablishedFixJob < CaseflowJob
ERROR_TEXT = "Claim not established."
EPECODES = %w[030 040 930 682].freeze

attr_reader :stuck_job_report_service

def initialize
@stuck_job_report_service = StuckJobReportService.new
end

def perform
return if decision_docs_with_errors.blank?

stuck_job_report_service.append_record_count(decision_docs_with_errors.count, ERROR_TEXT)

decision_docs_with_errors.each do |single_decision_document|
file_number = single_decision_document.veteran.file_number
epe_array = EndProductEstablishment.where(veteran_file_number: file_number)
validated_epes = epe_array.map { |epe| validate_epe(epe) }

stuck_job_report_service.append_single_record(single_decision_document.class.name, single_decision_document.id)

resolve_error_on_records(single_decision_document, validated_epes)
end

stuck_job_report_service.append_record_count(decision_docs_with_errors.count, ERROR_TEXT)
stuck_job_report_service.write_log_report(ERROR_TEXT)
end

def decision_docs_with_errors
DecisionDocument.where("error ILIKE ?", "%#{ERROR_TEXT}%")
end

def validate_epe(epe)
epe_code = epe&.code&.slice(0, 3)
EPECODES.include?(epe_code) && epe&.established_at.present?
end

private

# :reek:FeatureEnvy
def resolve_error_on_records(object_type, epes_array)
ActiveRecord::Base.transaction do
if !epes_array.include?(false)
object_type.clear_error!
end
rescue StandardError => error
log_error(error)
stuck_job_report_service.append_errors(object_type.class.name, object_type.id, error)
end
end
end
25 changes: 5 additions & 20 deletions app/jobs/contention_not_found_remediation_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
class ContentionNotFoundRemediationJob < CaseflowJob
queue_with_priority :low_priority

S3_FOLDER_NAME = "data-remediation-output"

def initialize
@logs = ["\nVBMS::ContentionNotFound Remediation Log"]
@remediated_request_issues_update_ids = []
@folder_name = (Rails.deploy_env == :prod) ? S3_FOLDER_NAME : "#{S3_FOLDER_NAME}-#{Rails.deploy_env}"
super
end

Expand Down Expand Up @@ -141,26 +144,8 @@ def sync_epe!(request_issues_update, request_issue, index)

# Save Logs to S3 Bucket
def store_logs_in_s3_bucket
# Set Client Resources for AWS
Aws.config.update(region: "us-gov-west-1")
s3client = Aws::S3::Client.new
s3resource = Aws::S3::Resource.new(client: s3client)
s3bucket = s3resource.bucket("data-remediation-output")

# Folder and File name
file_name = "contention-not-found-remediation-logs/cnf-remediation-log-#{Time.zone.now}"

# Store contents of logs array in a temporary file
content = @logs.join("\n")
temporary_file = Tempfile.new("cnf-log.txt")
filepath = temporary_file.path
temporary_file.write(content)
temporary_file.flush

# Store File in S3 bucket
s3bucket.object(file_name).upload_file(filepath, acl: "private", server_side_encryption: "AES256")

# Delete Temporary File
temporary_file.close!
file_name = "contention-not-found-remediation-logs/cnf-remediation-log-#{Time.zone.now}"
S3Service.store_file("#{@folder_name}/#{file_name}", content)
end
end
36 changes: 36 additions & 0 deletions app/jobs/dta_sc_creation_failed_fix_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class DtaScCreationFailedFixJob < CaseflowJob
ERROR_TEXT = "DTA SC Creation Failed"

# :reek:FeatureEnvy
def perform
stuck_job_report_service = StuckJobReportService.new
return if hlrs_with_errors.blank?

stuck_job_report_service.append_record_count(hlrs_with_errors.count, ERROR_TEXT)

hlrs_with_errors.each do |hlr|
next unless SupplementalClaim.find_by(
decision_review_remanded_id: hlr.id,
decision_review_remanded_type: "HigherLevelReview"
)

stuck_job_report_service.append_single_record(hlr.class.name, hlr.id)

ActiveRecord::Base.transaction do
hlr.clear_error!
rescue StandardError => error
log_error(error)
stuck_job_report_service.append_error(hlr.class.name, hlr.id, error)
end
end

stuck_job_report_service.append_record_count(hlrs_with_errors.count, ERROR_TEXT)
stuck_job_report_service.write_log_report(ERROR_TEXT)
end

def hlrs_with_errors
HigherLevelReview.where("establishment_error ILIKE ?", "%#{ERROR_TEXT}%")
end
end
2 changes: 1 addition & 1 deletion app/jobs/duplicate_ep_remediation_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class DuplicateEpRemediationJob < ApplicationJob
class DuplicateEpRemediationJob < CaseflowJob
queue_with_priority :low_priority
application_attr :intake
def perform
Expand Down
45 changes: 45 additions & 0 deletions app/jobs/sc_dta_for_appeal_fix_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class ScDtaForAppealFixJob < CaseflowJob
ERRORTEXT = "Can't create a SC DTA for appeal"

def records_with_errors
DecisionDocument.where("error ILIKE ?", "%#{ERRORTEXT}%")
end

def sc_dta_for_appeal_fix
stuck_job_report_service = StuckJobReportService.new
return if records_with_errors.blank?

# count of records with errors before fix
stuck_job_report_service.append_record_count(records_with_errors.count, ERRORTEXT)

records_with_errors.each do |decision_doc|
claimant = decision_doc.appeal.claimant

next unless claimant.payee_code.nil?

if claimant.type == "VeteranClaimant"
claimant.update!(payee_code: "00")
elsif claimant.type == "DependentClaimant"
claimant.update!(payee_code: "10")
end
stuck_job_report_service.append_single_record(decision_doc.class.name, decision_doc.id)
clear_error_on_record(decision_doc)
end

# record count with errors after fix
stuck_job_report_service.append_record_count(records_with_errors.count, ERRORTEXT)
stuck_job_report_service.write_log_report(ERRORTEXT)
end

# :reek:FeatureEnvy
def clear_error_on_record(decision_doc)
ActiveRecord::Base.transaction do
decision_doc.clear_error!
rescue StandardError => error
log_error(error)
stuck_job_report_service.append_errors(decision_doc.class.name, decision_doc.id, error)
end
end
end
Loading
Loading