Class: Bugwatch::DbQueryBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/bugwatch/db_query_buffer.rb

Constant Summary collapse

BATCH_SIZE =
50
FLUSH_INTERVAL =

seconds

15

Instance Method Summary collapse

Constructor Details

#initialize(config: Bugwatch.configuration) ⇒ DbQueryBuffer

Returns a new instance of DbQueryBuffer.



6
7
8
9
10
11
12
# File 'lib/bugwatch/db_query_buffer.rb', line 6

def initialize(config: Bugwatch.configuration)
  @config = config
  @sender = DbQuerySender.new(config: config)
  @mutex  = Mutex.new
  @buffer = []
  start_flusher
end

Instance Method Details

#flushObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bugwatch/db_query_buffer.rb', line 25

def flush
  batch = @mutex.synchronize do
    items = @buffer
    @buffer = []
    items
  end

  @sender.send_batch(batch) unless batch.empty?
rescue StandardError
  # Never let flushing break the app
end

#push(payload) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/bugwatch/db_query_buffer.rb', line 14

def push(payload)
  should_flush = false

  @mutex.synchronize do
    @buffer << payload
    should_flush = @buffer.size >= BATCH_SIZE
  end

  flush if should_flush
end

#shutdownObject



37
38
39
40
# File 'lib/bugwatch/db_query_buffer.rb', line 37

def shutdown
  stop_flusher
  flush
end