From 3710dd48501102dc4b773ea3f8ed9d13963d7670 Mon Sep 17 00:00:00 2001 From: Ian Ballou Date: Mon, 27 Jan 2025 21:11:38 +0000 Subject: [PATCH] Fixes #37600 - ignore missing content on RH repo deletion --- .../candlepin/environment/set_content.rb | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/lib/actions/candlepin/environment/set_content.rb b/app/lib/actions/candlepin/environment/set_content.rb index 1b3f794c196..b8fef12b88b 100644 --- a/app/lib/actions/candlepin/environment/set_content.rb +++ b/app/lib/actions/candlepin/environment/set_content.rb @@ -1,7 +1,11 @@ module Actions module Candlepin module Environment - class SetContent < Candlepin::Abstract + class SetContent < Actions::Base + middleware.use ::Actions::Middleware::RemoteAction + middleware.use ::Actions::Middleware::CandlepinServicesCheck + middleware.use ::Actions::Middleware::KeepSessionId + def plan(content_view, environment, content_view_environment, new_content_id = nil) plan_self(:content_view_id => content_view.id, :environment_id => environment.id, @@ -9,7 +13,7 @@ def plan(content_view, environment, content_view_environment, new_content_id = n :new_content_id => new_content_id) end - def finalize # rubocop:disable Metrics/AbcSize + def finalize # rubocop:disable Metrics/AbcSize, Metrics/MethodLength content_view = ::Katello::ContentView.find(input[:content_view_id]) environment = ::Katello::KTEnvironment.find(input[:environment_id]) content_ids = content_view.repos(environment).map(&:content_id).uniq.compact @@ -32,6 +36,14 @@ def finalize # rubocop:disable Metrics/AbcSize # Since its a dup id refresh the existing ids list (which hopefully will not have the duplicate content) # and try again. output[:add_ids] = content_ids - existing_ids + rescue RestClient::ResourceNotFound => e + # Set a higher limit for retries just in case the missing content is not being parsed from the error body correctly. + # If the content is not found after the retries, assume it is gone and continue. + raise e if ((retries += 1) == 1_000) + # Parse the missing content from the Candlepin response and remove it from the add_ids list. + missing_content = JSON.parse(e.response.body)['displayMessage'].split(' ')[-1].gsub(/"(.+?)"\./, '\1') + Rails.logger.debug "Content #{missing_content} not found in the environment. Removing it from the add_ids list." + output[:add_ids].delete(missing_content) end end retries = 0 @@ -39,8 +51,9 @@ def finalize # rubocop:disable Metrics/AbcSize begin output[:delete_response] = ::Katello::Resources::Candlepin::Environment.delete_content(input[:cp_environment_id], output[:delete_ids]) break - rescue RestClient::ResourceNotFound => e - raise e if ((retries += 1) == max_retries) + rescue RestClient::ResourceNotFound + # If the content is not found after the retries, assume it is gone and continue. + break if ((retries += 1) == max_retries) # Candlepin raises a 404 in case a content id is not found in this environment # If thats the case lets just refresh the existing ids list (which hopefully will not have the 404'd content) # and try again.