Class: PostContentUpdatedHandler

Inherits:
ApplicationJob show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/post_content_updated_handler.rb

Overview

Orchestrator for Events::PostContentUpdated.

Sequences all post-save side-effects in dependency order so there are no
races between the operations that read or mutate the post's HTML body:

  1. EmbeddedAssetSyncService — only when solution_changed is true.
    Backfills missing data-embedded-asset-uuid attributes. Mutates
    post.solution via update_columns, so a reload is required before any
    downstream step reads the body. Assistant blog tools also run this
    synchronously after saves to avoid races with the next patch/get round-trip.

    Note: when a save originates from the web editor (Crm::PostsController),
    the controller already ran this synchronously so step 1 is a fast no-op
    here (all UUIDs already present). For AI / API saves it does the real work.

  2. BlogSchemaExtractor — reads the now-stable HTML body to extract
    FAQPage/HowTo structured data. Skipped for drafts.

  3. purge_edge_cache — pushes the stable, UUID-annotated HTML to CDN.
    Runs after step 1 so the CDN never caches a version with missing UUIDs.

PostSitemapRecrawlHandler subscribes separately on the :low queue so slow
HTTP crawls don't block this job and don't compete with high-priority work.

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/subscribers/post_content_updated_handler.rb', line 31

def perform(event)
  post = Post.find_by(id: event.data[:post_id])
  return unless post

  # Step 1 — sync embedded assets only when the HTML body changed.
  # Reload first: for web-editor saves the controller already ran this
  # synchronously, so the DB may already have UUID-annotated HTML.
  # Reloading ensures we operate on the latest solution and the service
  # becomes a fast no-op if all UUIDs are already present.
  if event.data[:solution_changed]
    post.reload
    Posts::EmbeddedAssetSyncService.new(post).call
    post.reload  # pick up any UUID attributes written by the sync service
  end

  # Step 2 — extract structured schema from the stable HTML
  if post.state.in?(%w[published scheduled])
    BlogSchemaExtractor.new(post).process
  end

  # Step 3 — flush CDN edge cache after HTML is fully stable
  post.purge_edge_cache(include_indirect_associations: true)
rescue StandardError => e
  ErrorReporting.error(e)
  raise
end