Class: NurseAndrea::LogShipper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nurse_andrea/log_shipper.rb

Constant Summary collapse

MAX_QUEUE_SIZE =
10_000

Instance Method Summary collapse

Constructor Details

#initializeLogShipper

Returns a new instance of LogShipper.



10
11
12
13
14
# File 'lib/nurse_andrea/log_shipper.rb', line 10

def initialize
  @queue  = []
  @mutex  = Mutex.new
  @thread = nil
end

Instance Method Details

#enqueue(entry) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/nurse_andrea/log_shipper.rb', line 32

def enqueue(entry)
  flush_now = @mutex.synchronize do
    @queue.shift if @queue.size >= MAX_QUEUE_SIZE
    @queue << entry
    @queue.size >= NurseAndrea.config.batch_size
  end
  flush! if flush_now
end

#flush!Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nurse_andrea/log_shipper.rb', line 41

def flush!
  entries = @mutex.synchronize do
    return if @queue.empty?
    batch = @queue.dup
    @queue.clear
    batch
  end
  return if entries.nil? || entries.empty?

  ship(entries)
end

#running?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/nurse_andrea/log_shipper.rb', line 28

def running?
  @thread&.alive? || false
end

#start!Object



16
17
18
19
20
21
# File 'lib/nurse_andrea/log_shipper.rb', line 16

def start!
  return if @thread&.alive?
  @thread = Thread.new { flush_loop }
  @thread.abort_on_exception = false
  @thread.name = "NurseAndrea::LogShipper"
end

#stop!Object



23
24
25
26
# File 'lib/nurse_andrea/log_shipper.rb', line 23

def stop!
  @thread&.kill
  flush!
end