Class: TwoPercent::BulkProcessor

Inherits:
Object
  • Object
show all
Includes:
ValidatesUserGroupsPatch
Defined in:
lib/two_percent/bulk_processor.rb

Instance Method Summary collapse

Methods included from ValidatesUserGroupsPatch

#validate_patch_operations!

Constructor Details

#initialize(operations, correlation_id: nil) ⇒ BulkProcessor

Returns a new instance of BulkProcessor.



7
8
9
10
# File 'lib/two_percent/bulk_processor.rb', line 7

def initialize(operations, correlation_id: nil)
  @operations = operations.map(&:with_indifferent_access)
  @correlation_id = correlation_id
end

Instance Method Details

#dispatchObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/two_percent/bulk_processor.rb', line 12

def dispatch
  @operations.each do |operation|
    resource_type, id = parse_path(operation[:path])

    # Persist data to two_percent tables first (wrapped in transaction for bulk integrity).
    # The transaction commits when this block closes.
    record = nil
    ActiveRecord::Base.transaction do
      record = persist_bulk_operation(operation[:method], resource_type, id, operation[:data])
    end

    # Publish domain events only AFTER the transaction commits, so any subscriber
    # that reacts to the event can rely on the persisted row being visible.
    # Publishing is intentionally outside the transaction: a publish failure no
    # longer rolls back the write.
    # Note: DELETE operations don't return a record, but still need to publish events.
    next unless record || operation[:method] == "DELETE"

    publish_domain_event(operation[:method], resource_type, record, id)
  end
end