Class: JetstreamBridge::BatchPublisher

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

Overview

Batch publisher for efficient bulk event publishing.

BatchPublisher allows you to queue multiple events and publish them together, providing detailed results about successes and failures. Each event is published independently, so partial failures are possible.

Examples:

Publishing multiple events

results = JetstreamBridge.publish_batch do |batch|
  users.each do |user|
    batch.add(event_type: "user.created", payload: { id: user.id })
  end
end

puts "Published: #{results.successful_count}, Failed: #{results.failed_count}"
results.errors.each { |e| logger.error("Failed: #{e[:event_id]}") }

Checking for failures

results = JetstreamBridge.publish_batch do |batch|
  batch.add(event_type: "order.created", payload: { id: 1 })
  batch.add(event_type: "order.updated", payload: { id: 2 })
end

if results.failure?
  logger.warn "Some events failed to publish"
end

Defined Under Namespace

Classes: BatchResult

Instance Method Summary collapse

Constructor Details

#initialize(publisher = nil) ⇒ BatchPublisher

Returns a new instance of BatchPublisher.

Parameters:

  • publisher (Publisher, nil) (defaults to: nil)

    Publisher instance to use for each event. Defaults to a new Publisher instance.



101
102
103
104
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 101

def initialize(publisher = nil)
  @publisher = publisher || Publisher.new
  @events = []
end

Instance Method Details

#add(event_or_hash = nil, resource_type: nil, event_type: nil, payload: nil, **options) ⇒ self

Add an event to the batch

Parameters:

  • event_or_hash (Hash) (defaults to: nil)

    Event data

  • resource_type (String, nil) (defaults to: nil)

    Resource type

  • event_type (String, nil) (defaults to: nil)

    Event type

  • payload (Hash, nil) (defaults to: nil)

    Payload data

  • options (Hash)

    Additional options

Returns:

  • (self)

    Returns self for chaining



114
115
116
117
118
119
120
121
122
123
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 114

def add(event_or_hash = nil, resource_type: nil, event_type: nil, payload: nil, **options)
  @events << {
    event_or_hash: event_or_hash,
    resource_type: resource_type,
    event_type: event_type,
    payload: payload,
    options: options
  }
  self
end

#empty?Boolean

Whether the batch has no queued events.

Returns:

  • (Boolean)


164
165
166
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 164

def empty?
  @events.empty?
end

#publishBatchResult

Publish all events in the batch

Returns:

  • (BatchResult)

    Result containing success/failure counts



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 128

def publish
  results = @events.map do |event_data|
    @publisher.publish(
      event_data[:event_or_hash],
      resource_type: event_data[:resource_type],
      event_type: event_data[:event_type],
      payload: event_data[:payload],
      **event_data[:options]
    )
  rescue StandardError => e
    Models::PublishResult.new(
      success: false,
      event_id: 'unknown',
      subject: 'unknown',
      error: e
    )
  end

  BatchResult.new(results)
ensure
  @events.clear
end

#sizeInteger Also known as: count, length

Get number of events queued

Returns:

  • (Integer)

    Number of events in batch



154
155
156
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 154

def size
  @events.size
end