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

[VAMC-19452]Update Alternative Banners DB every 10min with sidekiq job #19550

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,9 @@ features:
show_yellow_ribbon_table:
actor_type: user
description: Used to show yellow ribbon table in Comparison Tool
banner_update_alternative_banners:
actor_type: user
description: Used to toggle the DB updating of alternative banners
banner_use_alternative_banners:
actor_type: user
description: Used to toggle use of alternative banners.
Expand Down
3 changes: 3 additions & 0 deletions lib/periodic_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
# Checks status of Flipper features expected to be enabled and alerts to Slack if any are not enabled
mgr.register('0 2,9,16 * * 1-5', 'AppealsApi::FlipperStatusAlert')

# Update alternative Banners data every 10 minutes
mgr.register('*/10 * * * *', 'Banners::UpdateAll')

# Update static data cache
mgr.register('0 0 * * *', 'Crm::TopicsDataJob')

Expand Down
56 changes: 56 additions & 0 deletions modules/banners/app/sidekiq/banners/update_all_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module Banners
class UpdateAllJob
include Sidekiq::Job

STATSD_KEY_PREFIX = 'banners.sidekiq.update_all_banners'

sidekiq_options retry: 5

sidekiq_retries_exhausted do |msg, _ex|
job_id = msg['jid']
job_class = msg['class']
error_class = msg['error_class']
error_message = msg['error_message']

StatsD.increment("#{STATSD_KEY_PREFIX}.exhausted")

message = "#{job_class} retries exhausted"
Rails.logger.error(message, { job_id:, error_class:, error_message: })
rescue => e
Rails.logger.error(
"Failure in #{job_class}#sidekiq_retries_exhausted",
{
messaged_content: e.message,
job_id:,
pre_exhaustion_failure: {
error_class:,
error_message:
}
}
)

raise e
end

def perform
return unless enabled?

Banners.update_all
rescue Banners::Updater::BannerDataFetchError => e
StatsD.increment("#{STATSD_KEY_PREFIX}.banner_data_fetch_error")
Rails.logger.error(
'Banner data fetch failed',
{ error_message: e.message, error_class: e.class.name }
)
raise e # Re-raise to trigger Sidekiq retries
end

private

def enabled?
Flipper.enabled?(:banner_update_alternative_banners)
end
end
end
6 changes: 4 additions & 2 deletions modules/banners/lib/banners/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Banners
class Updater
STATSD_KEY_PREFIX = 'banners.updater'

class BannerDataFetchError < StandardError; end

# If banners are to be added in the future, include them here by adding
# another #update_{type_of}_banners method for #perform to call
def self.perform
Expand Down Expand Up @@ -70,8 +72,8 @@ def vamcs_banner_data

begin
JSON.parse(response.body).dig('data', 'nodeQuery', 'entities')
rescue JSON::ParserError
[]
rescue JSON::ParserError => e
raise BannerDataFetchError, "Failed to parse VAMC banner data: #{e.message}"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions modules/banners/spec/lib/updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
context 'when response is invalid JSON' do
let(:response_body) { 'invalid json' }

it 'returns empty array' do
expect(updater.send(:vamcs_banner_data)).to eq([])
it 'returns an error' do
expect { updater.send(:vamcs_banner_data) }.to raise_error(Banners::Updater::BannerDataFetchError)
end
end
end
Expand Down
Loading