Class: JetstreamBridge::BatchPublisher
- Inherits:
-
Object
- Object
- JetstreamBridge::BatchPublisher
- 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.
Defined Under Namespace
Classes: BatchResult
Instance Method Summary collapse
-
#add(event_or_hash = nil, resource_type: nil, event_type: nil, payload: nil, **options) ⇒ self
Add an event to the batch.
- #empty? ⇒ Boolean
-
#initialize(publisher = nil) ⇒ BatchPublisher
constructor
A new instance of BatchPublisher.
-
#publish ⇒ BatchResult
Publish all events in the batch.
-
#size ⇒ Integer
(also: #count, #length)
Get number of events queued.
Constructor Details
#initialize(publisher = nil) ⇒ BatchPublisher
99 100 101 102 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 99 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
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 112 def add(event_or_hash = nil, resource_type: nil, event_type: nil, payload: nil, **) @events << { event_or_hash: event_or_hash, resource_type: resource_type, event_type: event_type, payload: payload, options: } self end |
#empty? ⇒ Boolean
159 160 161 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 159 def empty? @events.empty? end |
#publish ⇒ BatchResult
Publish all events in the batch
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 126 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 |
#size ⇒ Integer Also known as: count, length
Get number of events queued
152 153 154 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 152 def size @events.size end |