Skip to content

Commit

Permalink
Fixes #36563 - Move remove orphan content units to async task
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 committed Jul 10, 2023
1 parent 009f5c6 commit 9dd0f85
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/lib/actions/katello/repository/index_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/models/katello/concerns/pulp_database_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion app/models/katello/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/models/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9dd0f85

Please sign in to comment.