From 9dd0f85ebc86749505d576f784b5de482bd0a896 Mon Sep 17 00:00:00 2001 From: Samir Jha Date: Fri, 7 Jul 2023 16:41:41 +0000 Subject: [PATCH] Fixes #36563 - Move remove orphan content units to async task --- .../remove_orphaned_content_units.rb | 32 +++++++++++++++++++ .../katello/repository/index_content.rb | 4 +++ .../katello/concerns/pulp_database_unit.rb | 4 +-- app/models/katello/repository.rb | 1 - test/models/repository_test.rb | 2 +- 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 app/lib/actions/katello/orphan_cleanup/remove_orphaned_content_units.rb diff --git a/app/lib/actions/katello/orphan_cleanup/remove_orphaned_content_units.rb b/app/lib/actions/katello/orphan_cleanup/remove_orphaned_content_units.rb new file mode 100644 index 00000000000..02504aa2c66 --- /dev/null +++ b/app/lib/actions/katello/orphan_cleanup/remove_orphaned_content_units.rb @@ -0,0 +1,32 @@ +module Actions + module Katello + module OrphanCleanup + class RemoveOrphanedContentUnits < Actions::Base + def plan(options = {}) + plan_self(id: options[:repo_id], destroy_all: options[:destroy_all]) + end + + def run + content_types_to_index = [] + if input[:destroy_all] + ::Katello::RepositoryTypeManager.enabled_repository_types.each_value do |repo_type| + content_types_to_index << repo_type.content_types_to_index + end + elsif input[:id] + repo = ::Katello::Repository.find(input[:id]) + content_types_to_index = repo.repository_type.content_types_to_index + else + fail "Pass either a repository to determine content type or destroy_all to destroy all orphaned content units" + end + content_types_to_index.flatten.each do |type| + type.model_class.orphaned.destroy_all + end + end + + def rescue_strategy + Dynflow::Action::Rescue::Skip + end + end + end + end +end diff --git a/app/lib/actions/katello/repository/index_content.rb b/app/lib/actions/katello/repository/index_content.rb index 0d6eb6f358a..c6b5f77e95d 100644 --- a/app/lib/actions/katello/repository/index_content.rb +++ b/app/lib/actions/katello/repository/index_content.rb @@ -31,6 +31,10 @@ def run output[:new_content][content_type.label] = new_count - initial_counts[content_type.label] end end + + def finalize + ForemanTasks.async_task(OrphanCleanup::RemoveOrphanedContentUnits, {repo_id: input[:id]}) + end end end end diff --git a/app/models/katello/concerns/pulp_database_unit.rb b/app/models/katello/concerns/pulp_database_unit.rb index 382e196e8cb..675d28e3159 100644 --- a/app/models/katello/concerns/pulp_database_unit.rb +++ b/app/models/katello/concerns/pulp_database_unit.rb @@ -169,9 +169,9 @@ def with_identifiers(ids) def orphaned if many_repository_associations - where.not(:id => repository_association_class.where(:repository_id => ::Katello::Repository.all).select(unit_id_field)) + where.not(:id => repository_association_class.select(unit_id_field)) else - where.not(:repository_id => ::Katello::Repository.all) + where.not(:repository_id => ::Katello::Repository.select(:id)) end end diff --git a/app/models/katello/repository.rb b/app/models/katello/repository.rb index ccd76734ba6..dd82b3caafb 100644 --- a/app/models/katello/repository.rb +++ b/app/models/katello/repository.rb @@ -935,7 +935,6 @@ def index_content(options = {}) repository_type.content_types_to_index.each do |type| Katello::Logging.time("CONTENT_INDEX", data: {type: type.model_class}) do Katello::ContentUnitIndexer.new(content_type: type, repository: self, optimized: !full_index).import_all - type.model_class.orphaned.destroy_all end end repository_type.index_additional_data_proc&.call(self) diff --git a/test/models/repository_test.rb b/test/models/repository_test.rb index 8a4742ce322..ffbbc208450 100644 --- a/test/models/repository_test.rb +++ b/test/models/repository_test.rb @@ -715,7 +715,7 @@ def test_index_content_destroys_orphans ::Katello::ContentUnitIndexer.any_instance.stubs(:import_all).returns(true) ::Katello::Repository.any_instance.stubs(:import_distribution_data).returns(true) rpm.repository_rpms.destroy_all - @rhel6.index_content + ForemanTasks.sync_task(Actions::Katello::OrphanCleanup::RemoveOrphanedContentUnits, {repo_id: @rhel6.id}) assert_raises(ActiveRecord::RecordNotFound) { rpm.reload } end