Class: Seekmodo::Sdk::Events::EventsQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/seekmodo/sdk/events/events_queue.rb

Constant Summary collapse

DEFAULT_AUTO_FLUSH_THRESHOLD =
50
DEFAULT_MAX_PER_FLUSH =
200

Instance Method Summary collapse

Constructor Details

#initialize(client, store, auto_flush_threshold: DEFAULT_AUTO_FLUSH_THRESHOLD, max_per_flush: DEFAULT_MAX_PER_FLUSH) ⇒ EventsQueue

Returns a new instance of EventsQueue.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/seekmodo/sdk/events/events_queue.rb', line 13

def initialize(
  client,
  store,
  auto_flush_threshold: DEFAULT_AUTO_FLUSH_THRESHOLD,
  max_per_flush: DEFAULT_MAX_PER_FLUSH
)
  @client = client
  @store = store
  @auto_flush_threshold = auto_flush_threshold
  @max_per_flush = max_per_flush
end

Instance Method Details

#flushObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/seekmodo/sdk/events/events_queue.rb', line 30

def flush
  batch = @store.drain(@max_per_flush)
  return 0 if batch.empty?

  begin
    @client.events(batch)
  rescue StandardError
    batch.each { |event| @store.push(event) }
    return 0
  end

  batch.length
end

#pending_countObject



44
45
46
# File 'lib/seekmodo/sdk/events/events_queue.rb', line 44

def pending_count
  @store.count
end

#push(event) ⇒ Object



25
26
27
28
# File 'lib/seekmodo/sdk/events/events_queue.rb', line 25

def push(event)
  @store.push(event)
  flush if @store.count >= @auto_flush_threshold
end