Class: Collavre::CreativeBroadcastJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/collavre/creative_broadcast_job.rb

Overview

Asynchronous broadcast for creative CRUD changes. Offloads per-user payload building and WebSocket delivery from the request cycle.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.svg_cacheObject



245
246
247
# File 'app/jobs/collavre/creative_broadcast_job.rb', line 245

def svg_cache
  @svg_cache ||= {}
end

Instance Method Details

#perform(creative_id, action, current_user_id: nil, payload: {}, options: {}) ⇒ Object

Parameters:

  • creative_id (Integer, Array<Integer>)

    single ID or array of IDs (for batch_created)

  • action (String)

    “created”, “updated”, “destroyed”, or “batch_created”

  • current_user_id (Integer, nil) (defaults to: nil)

    user who triggered the change (excluded from broadcast)

  • payload (Hash) (defaults to: {})

    pre-built node payload (for created/updated) or destroy payload

  • options (Hash) (defaults to: {})

    extra options (after_id, destroy_users, destroy_linked_map)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/jobs/collavre/creative_broadcast_job.rb', line 16

def perform(creative_id, action, current_user_id: nil, payload: {}, options: {})
  case action
  when "batch_created"
    # creative_id is an array of IDs in tree order (parent before child).
    # Process sequentially within a single job to guarantee ordering.
    creative_ids = Array(creative_id)
    creative_ids.each do |cid|
      creative = Creative.find_by(id: cid)
      next unless creative

      creative.reload
      node_payload = creative.broadcast_node_payload
      node_payload[:previous_sibling_id] = creative.previous_sibling&.id
      broadcast_change(creative, "created", current_user_id, node_payload)
    end
    nil
  when "created", "updated"
    creative = Creative.find_by(id: creative_id)
    return unless creative

    broadcast_change(creative, action, current_user_id, payload.deep_symbolize_keys)
  when "destroyed"
    broadcast_destroyed(
      creative_id,
      current_user_id,
      payload.deep_symbolize_keys,
      options.deep_symbolize_keys
    )
  end
rescue StandardError => e
  Rails.logger.error "[CreativeBroadcastJob] ERROR #{action} creative##{creative_id}: #{e.message}\n#{e.backtrace.first(5).join("\n")}"
end