Class: SagaForge::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/saga_forge/publisher.rb

Overview

The external entry point: INSERTs join any open transaction on the engine connection; enqueues happen right after in plain code. The pending row is the obligation, the enqueue a hint, the sweeper the guarantee.

Idempotency is structural: the (saga_class, correlation_id, event_name) unique index no-ops a duplicate delivery. A forward-only saga handles each event name at most once, so a second delivery is always a duplicate.

Class Method Summary collapse

Class Method Details

.publish(event_name, payload:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/saga_forge/publisher.rb', line 11

def publish(event_name, payload:)
  if SagaForge.within_saga_execution?
    raise UnstagedPublishError,
      "SagaForge.publish called inside saga execution — use saga.publish (staged, delivered on commit)"
  end

  attrs_list = Router.resolve(event_name, payload)
  return [] if attrs_list.empty?

  rows = attrs_list.filter_map { |attrs| insert_row(attrs) }
  rows.each { |row| ExecutionJob.perform_later(row.id) }
  rows
end