Class: SolidObserver::Services::FlushEventBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_observer/services/flush_event_buffer.rb

Overview

Flushes buffered events to the database using bulk insert.

Attempts bulk insert with automatic fallback to smaller batches if the initial insert fails.

Examples:

Flush events

FlushEventBuffer.call(events)

Constant Summary collapse

BATCH_SIZE =
100

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events) ⇒ FlushEventBuffer

Returns a new instance of FlushEventBuffer.



21
22
23
24
# File 'lib/solid_observer/services/flush_event_buffer.rb', line 21

def initialize(events)
  @events = events
  @failed_count = 0
end

Class Method Details

.call(events) ⇒ Integer

Returns Number of events successfully inserted.

Parameters:

  • events (Array<Hash>)

    Array of event data to insert

Returns:

  • (Integer)

    Number of events successfully inserted



17
18
19
# File 'lib/solid_observer/services/flush_event_buffer.rb', line 17

def self.call(events)
  new(events).call
end

Instance Method Details

#callObject



26
27
28
29
30
31
32
33
# File 'lib/solid_observer/services/flush_event_buffer.rb', line 26

def call
  return 0 if @events.empty?

  bulk_insert
  @events.size
rescue ActiveRecord::RecordInvalid, ActiveRecord::StatementInvalid => e
  handle_bulk_insert_failure(e)
end