Class: LogStashLogger::Device::Redis

Inherits:
Connectable show all
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.



10
11
12
13
14
15
16
17
# File 'lib/logstash-logger/device/redis.rb', line 10

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

  normalize_path(opts)

  @redis_options = opts
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



8
9
10
# File 'lib/logstash-logger/device/redis.rb', line 8

def list
  @list
end

Instance Method Details

#closeObject



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

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

#connectObject



19
20
21
# File 'lib/logstash-logger/device/redis.rb', line 19

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

#flush(*args) ⇒ Object



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

def flush(*args)
  if args.empty?
    buffer_flush
  else
    messages, list = *args
    write_batch(messages, list)
  end
end

#reconnectObject



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

def reconnect
  @io.client.reconnect
end

#with_connectionObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logstash-logger/device/redis.rb', line 27

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

#write(message) ⇒ Object



39
40
41
42
# File 'lib/logstash-logger/device/redis.rb', line 39

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

#write_batch(messages, list = nil) ⇒ Object



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

def write_batch(messages, list = nil)
  with_connection do
    @io.rpush(list, messages)
  end
end