Class: LogStashLogger::Device::Redis

Inherits:
Connectable show all
Includes:
Stud::Buffer
Defined in:
lib/logstash-logger/device/redis.rb

Constant Summary collapse

DEFAULT_LIST =
'logstash'

Instance Attribute Summary collapse

Attributes inherited from Base

#io, #sync

Instance Method Summary collapse

Methods inherited from Connectable

#connected?, #to_io

Methods inherited from Base

#to_io

Constructor Details

#initialize(opts) ⇒ Redis

Returns a new instance of Redis.



13
14
15
16
17
18
19
20
21
22
# File 'lib/logstash-logger/device/redis.rb', line 13

def initialize(opts)
  super
  @list = opts.delete(:list) || DEFAULT_LIST
  @redis_options = opts

  @batch_events = opts.fetch(:batch_events, 50)
  @batch_timeout = opts.fetch(:batch_timeout, 5)

  buffer_initialize max_items: @batch_events, max_interval: @batch_timeout
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



11
12
13
# File 'lib/logstash-logger/device/redis.rb', line 11

def list
  @list
end

Instance Method Details

#closeObject



48
49
50
51
52
53
54
55
# File 'lib/logstash-logger/device/redis.rb', line 48

def close
  buffer_flush(final: true)
  @io && @io.quit
rescue => e
  warn "#{self.class} - #{e.class} - #{e.message}"
ensure
  @io = nil
end

#connectObject



24
25
26
# File 'lib/logstash-logger/device/redis.rb', line 24

def connect
  @io = ::Redis.new(@redis_options)
end

#flush(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/logstash-logger/device/redis.rb', line 57

def flush(*args)
  if args.empty?
    buffer_flush
  else
    messages, list = *args
    with_connection do
      @io.rpush(list, messages)
    end
  end
end

#reconnectObject



28
29
30
# File 'lib/logstash-logger/device/redis.rb', line 28

def reconnect
  @io.client.reconnect
end

#with_connectionObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/logstash-logger/device/redis.rb', line 32

def with_connection
  connect unless @io
  yield
rescue ::Redis::InheritedError
  reconnect
  retry
rescue => e
  warn "#{self.class} - #{e.class} - #{e.message}"
  @io = nil
end

#write(message) ⇒ Object



43
44
45
46
# File 'lib/logstash-logger/device/redis.rb', line 43

def write(message)
  buffer_receive message, @list
  buffer_flush(force: true) if @sync
end