-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/APPEALS-15376
- Loading branch information
Showing
69 changed files
with
3,268 additions
and
22 deletions.
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
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
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
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
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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# This job will search for and reprocess unfinished Batch Processes nightly. | ||
# Search Criteria is for Batch Processes that are in an unfinished state ('PRE_PROCESSING', 'PROCESSING') & | ||
# have a created_at date/time that is greater than the ERROR_DELAY defined within batch_process.rb | ||
class BatchProcessRescueJob < CaseflowJob | ||
queue_with_priority :low_priority | ||
|
||
# :reek:FeatureEnvy | ||
def perform | ||
batches = BatchProcess.needs_reprocessing | ||
if batches.any? | ||
batches.each do |batch| | ||
begin | ||
batch.process_batch! | ||
rescue StandardError => error | ||
log_error(error, extra: { active_job_id: job_id.to_s, job_time: Time.zone.now.to_s }) | ||
slack_msg = "Error running #{self.class.name}. Error: #{error.message}. Active Job ID: #{job_id}." | ||
slack_msg += " See Sentry event #{Raven.last_event_id}." if Raven.last_event_id.present? | ||
slack_service.send_notification("[ERROR] #{slack_msg}", self.class.to_s) | ||
next | ||
end | ||
end | ||
else | ||
Rails.logger.info("No Unfinished Batches Could Be Identified. Time: #{Time.zone.now}.") | ||
end | ||
end | ||
end |
82 changes: 82 additions & 0 deletions
82
app/jobs/batch_processes/priority_ep_sync_batch_process_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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
class PriorityEpSyncBatchProcessJob < CaseflowJob | ||
queue_with_priority :low_priority | ||
|
||
# Using macro-style definition. The locking scope will be TheClass#method and only one method can run at any | ||
# given time. | ||
include RedisMutex::Macro | ||
|
||
# Default options for RedisMutex#with_lock | ||
# :block => 1 # Specify in seconds how long you want to wait for the lock to be released. | ||
# # Specify 0 if you need non-blocking sematics and return false immediately. (default: 1) | ||
# :sleep => 0.1 # Specify in seconds how long the polling interval should be when :block is given. | ||
# # It is NOT recommended to go below 0.01. (default: 0.1) | ||
# :expire => 10 # Specify in seconds when the lock should be considered stale when something went wrong | ||
# # with the one who held the lock and failed to unlock. (default: 10) | ||
# | ||
# RedisMutex.with_lock("PriorityEpSyncBatchProcessJob", block: 60, expire: 100) | ||
# Key => "PriorityEpSyncBatchProcessJob" | ||
|
||
JOB_DURATION ||= ENV["BATCH_PROCESS_JOB_DURATION"].to_i.minutes | ||
SLEEP_DURATION ||= ENV["BATCH_PROCESS_SLEEP_DURATION"].to_i | ||
|
||
# Attempts to create & process batches for 50 minutes | ||
# There will be a 5 second rest between each iteration | ||
# Job will end if there are no records are left to batch | ||
|
||
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity | ||
def perform | ||
setup_job | ||
loop do | ||
break if job_running_past_expected_end_time? || should_stop_job | ||
|
||
begin | ||
batch = nil | ||
RedisMutex.with_lock("PriorityEpSyncBatchProcessJob", block: 60, expire: 100) do | ||
batch = ActiveRecord::Base.transaction do | ||
records_to_batch = PriorityEpSyncBatchProcess.find_records_to_batch | ||
next if records_to_batch.empty? | ||
|
||
PriorityEpSyncBatchProcess.create_batch!(records_to_batch) | ||
end | ||
end | ||
|
||
batch ? batch.process_batch! : stop_job(log_no_records_found: true) | ||
|
||
sleep(SLEEP_DURATION) | ||
rescue StandardError => error | ||
log_error(error, extra: { job_id: job_id.to_s, job_time: Time.zone.now.to_s }) | ||
slack_msg = "Error running #{self.class.name}. Error: #{error.message}. Active Job ID: #{job_id}." | ||
slack_msg += " See Sentry event #{Raven.last_event_id}." if Raven.last_event_id.present? | ||
slack_service.send_notification("[ERROR] #{slack_msg}", self.class.to_s) | ||
stop_job | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity | ||
|
||
private | ||
|
||
attr_accessor :job_expected_end_time, :should_stop_job | ||
|
||
def setup_job | ||
RequestStore.store[:current_user] = User.system_user | ||
@should_stop_job = false | ||
@job_expected_end_time = Time.zone.now + JOB_DURATION | ||
end | ||
|
||
def job_running_past_expected_end_time? | ||
Time.zone.now > job_expected_end_time | ||
end | ||
|
||
# :reek:BooleanParameter | ||
# :reek:ControlParameter | ||
def stop_job(log_no_records_found: false) | ||
self.should_stop_job = true | ||
if log_no_records_found | ||
Rails.logger.info("#{self.class} Cannot Find Any Records to Batch."\ | ||
" Job will be enqueued again at the top of the hour. Active Job ID: #{job_id}. Time: #{Time.zone.now}") | ||
end | ||
end | ||
end |
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,89 @@ | ||
# frozen_string_literal: true | ||
|
||
# This job will find deltas between the end product establishment table and the VBMS ext claim table | ||
# where VBMS ext claim level status code is CLR or CAN. If EP is already in the queue it will be skipped. | ||
# Job will populate queue ENV["END_PRODUCT_QUEUE_BATCH_LIMIT"] records at a time. | ||
# This job will run on a 50 minute loop, sleeping for 5 seconds between iterations. | ||
class PopulateEndProductSyncQueueJob < CaseflowJob | ||
queue_with_priority :low_priority | ||
|
||
JOB_DURATION ||= ENV["END_PRODUCT_QUEUE_JOB_DURATION"].to_i.minutes | ||
SLEEP_DURATION ||= ENV["END_PRODUCT_QUEUE_SLEEP_DURATION"].to_i | ||
BATCH_LIMIT ||= ENV["END_PRODUCT_QUEUE_BATCH_LIMIT"].to_i | ||
|
||
# rubocop:disable Metrics/CyclomaticComplexity | ||
def perform | ||
setup_job | ||
loop do | ||
break if job_running_past_expected_end_time? || should_stop_job | ||
|
||
begin | ||
batch = ActiveRecord::Base.transaction do | ||
priority_epes = find_priority_end_product_establishments_to_sync | ||
next if priority_epes.empty? | ||
|
||
priority_epes | ||
end | ||
|
||
batch ? insert_into_priority_sync_queue(batch) : stop_job(log_no_records_found: true) | ||
|
||
sleep(SLEEP_DURATION) | ||
rescue StandardError => error | ||
log_error(error, extra: { active_job_id: job_id.to_s, job_time: Time.zone.now.to_s }) | ||
slack_msg = "Error running #{self.class.name}. Error: #{error.message}. Active Job ID: #{job_id}." | ||
slack_msg += " See Sentry event #{Raven.last_event_id}." if Raven.last_event_id.present? | ||
slack_service.send_notification("[ERROR] #{slack_msg}", self.class.to_s) | ||
stop_job | ||
end | ||
end | ||
end | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
|
||
private | ||
|
||
attr_accessor :job_expected_end_time, :should_stop_job | ||
|
||
def find_priority_end_product_establishments_to_sync | ||
get_batch = <<-SQL | ||
select id | ||
from end_product_establishments | ||
inner join vbms_ext_claim | ||
on end_product_establishments.reference_id = vbms_ext_claim."CLAIM_ID"::varchar | ||
where (end_product_establishments.synced_status <> vbms_ext_claim."LEVEL_STATUS_CODE" or end_product_establishments.synced_status is null) | ||
and vbms_ext_claim."LEVEL_STATUS_CODE" in ('CLR','CAN') | ||
and end_product_establishments.id not in (select end_product_establishment_id from priority_end_product_sync_queue) | ||
limit #{BATCH_LIMIT}; | ||
SQL | ||
|
||
ActiveRecord::Base.connection.exec_query(ActiveRecord::Base.sanitize_sql(get_batch)).rows.flatten | ||
end | ||
|
||
def insert_into_priority_sync_queue(batch) | ||
batch.each do |ep_id| | ||
PriorityEndProductSyncQueue.create!( | ||
end_product_establishment_id: ep_id | ||
) | ||
end | ||
Rails.logger.info("PopulateEndProductSyncQueueJob EPEs processed: #{batch} - Time: #{Time.zone.now}") | ||
end | ||
|
||
def setup_job | ||
RequestStore.store[:current_user] = User.system_user | ||
@should_stop_job = false | ||
@job_expected_end_time = Time.zone.now + JOB_DURATION | ||
end | ||
|
||
def job_running_past_expected_end_time? | ||
Time.zone.now > job_expected_end_time | ||
end | ||
|
||
# :reek:BooleanParameter | ||
# :reek:ControlParameter | ||
def stop_job(log_no_records_found: false) | ||
self.should_stop_job = true | ||
if log_no_records_found | ||
Rails.logger.info("PopulateEndProductSyncQueueJob is not able to find any batchable EPE records."\ | ||
" Active Job ID: #{job_id}. Time: #{Time.zone.now}") | ||
end | ||
end | ||
end |
Oops, something went wrong.