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
Whether the batch has no queued events.
-
#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
Returns a new instance of BatchPublisher.
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
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, **) @events << { event_or_hash: event_or_hash, resource_type: resource_type, event_type: event_type, payload: payload, options: } self end |
#empty? ⇒ Boolean
Whether the batch has no queued events.
164 165 166 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 164 def empty? @events.empty? end |
#publish ⇒ BatchResult
Publish all events in the batch
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 |
#size ⇒ Integer Also known as: count, length
Get number of events queued
154 155 156 |
# File 'lib/jetstream_bridge/publisher/batch_publisher.rb', line 154 def size @events.size end |