Class: Maintenance::EmbeddingOrphanCleanup

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/maintenance/embedding_orphan_cleanup.rb

Overview

Removes ContentEmbedding rows for records that are no longer eligible
(unpublished posts, inactive images, discontinued items, etc.)

The content_embeddings partitions have FK CASCADE DELETE constraints, so
hard-deleted parent records clean up automatically. This service handles the
softer case: records still in the DB but no longer active/published/eligible.

Each embeddable type is cleaned with a single partitioned DELETE, so the
whole service typically completes in milliseconds.

Eligibility rules mirror Admin::EmbeddingsController#build_stats — what the
dashboard counts as "orphaned" is exactly what this service removes.

Examples:

Manual invocation

Maintenance::EmbeddingOrphanCleanup.new.process

Via ServiceRunner

ServiceRunner.perform_async('Maintenance::EmbeddingOrphanCleanup')

Constant Summary collapse

EMBEDDABLE_TYPES =
%w[
  Post
  Article
  Showcase
  Video
  Image
  SiteMap
  ReviewsIo
  Item
  ProductLine
  CallRecord
  Activity
  AssistantBrainEntry
].freeze

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#processObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/services/maintenance/embedding_orphan_cleanup.rb', line 38

def process
  total_deleted = 0

  EMBEDDABLE_TYPES.each do |type|
    scope = eligible_scope(type)
    next unless scope

    deleted = ContentEmbedding
              .where(embeddable_type: type)
              .where.not(embeddable_id: scope)
              .delete_all

    log_info "#{type}: #{deleted} orphan embedding(s) deleted" if deleted.positive?
    total_deleted += deleted
  end

  log_info "Orphan cleanup complete — #{total_deleted} total embedding(s) removed"
  total_deleted
end