Class: Ration::Backends::Memory

Inherits:
Base
  • Object
show all
Defined in:
lib/ration/backends/memory.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_MAX_PAYLOAD_BYTES

Instance Method Summary collapse

Methods inherited from Base

#on_event

Constructor Details

#initialize(max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES, sync: false, logger: nil) ⇒ Memory

Returns a new instance of Memory.



7
8
9
10
11
12
13
14
# File 'lib/ration/backends/memory.rb', line 7

def initialize(max_payload_bytes: DEFAULT_MAX_PAYLOAD_BYTES, sync: false, logger: nil)
  super()
  @max_payload_bytes = max_payload_bytes
  @sync              = sync
  @logger            = logger || Logger.new($stderr)
  @queue             = nil
  @thread            = nil
end

Instance Method Details

#publish(event) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ration/backends/memory.rb', line 16

def publish(event)
  check_payload_size!(event.to_json, @max_payload_bytes)

  if @sync
    emit(event)
  else
    @queue.push(event)
  end
end

#startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ration/backends/memory.rb', line 26

def start
  return if @sync
  return if @thread

  @queue  = Queue.new
  @thread = Thread.new {
    while (event = @queue.pop)
      begin
        emit(event)
      rescue => e
        @logger.error("Ration::Backends::Memory listener error: #{e.class}: #{e.message}")
      end
    end
  }
end

#stopObject



42
43
44
45
46
47
48
49
# File 'lib/ration/backends/memory.rb', line 42

def stop
  return if @sync

  @queue&.close
  @thread&.join
  @queue  = nil
  @thread = nil
end