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
23
24
25
# File 'lib/logstash-logger/device/redis.rb', line 13

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

  normalize_path(opts)

  @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



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

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

#connectObject



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

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

#flush(*args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/logstash-logger/device/redis.rb', line 61

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

#reconnectObject



32
33
34
# File 'lib/logstash-logger/device/redis.rb', line 32

def reconnect
  @io.client.reconnect
end

#with_connectionObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/logstash-logger/device/redis.rb', line 36

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



47
48
49
50
# File 'lib/logstash-logger/device/redis.rb', line 47

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