Class: LogStashLogger::Device::Connectable

Inherits:
Base
  • Object
show all
Includes:
Stud::Buffer
Defined in:
lib/logstash-logger/device/connectable.rb

Direct Known Subclasses

Kafka, Redis, Socket, Unix

Instance Attribute Summary

Attributes inherited from Base

#io, #sync

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connectable

Returns a new instance of Connectable.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/logstash-logger/device/connectable.rb', line 8

def initialize(opts = {})
  super

  if opts[:batch_events]
    warn "The :batch_events option is deprecated. Please use :buffer_max_items instead"
  end

  if opts[:batch_timeout]
    warn "The :batch_timeout option is deprecated. Please use :buffer_max_interval instead"
  end

  @buffer_max_items = opts[:batch_events] || opts[:buffer_max_items]
  @buffer_max_interval = opts[:batch_timeout] || opts[:buffer_max_interval]

  buffer_initialize max_items: @buffer_max_items, max_interval: @buffer_max_interval
end

Instance Method Details

#closeObject



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

def close
  buffer_flush(final: true)
  super
end

#connectObject

Implemented by subclasses



62
63
64
# File 'lib/logstash-logger/device/connectable.rb', line 62

def connect
  fail NotImplementedError
end

#connected?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/logstash-logger/device/connectable.rb', line 49

def connected?
  !!@io
end

#flush(*args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/logstash-logger/device/connectable.rb', line 30

def flush(*args)
  if args.empty?
    buffer_flush
  else
    write_batch(args[0])
  end
end

#reconnectObject



66
67
68
69
# File 'lib/logstash-logger/device/connectable.rb', line 66

def reconnect
  @io = nil
  connect
end

#to_ioObject



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

def to_io
  with_connection do
    @io
  end
end

#with_connection(&block) ⇒ Object

Ensure the block is executed with a valid connection



72
73
74
75
76
77
78
79
# File 'lib/logstash-logger/device/connectable.rb', line 72

def with_connection(&block)
  connect unless connected?
  yield
rescue => e
  warn "#{self.class} - #{e.class} - #{e.message}"
  @io = nil
  raise
end

#write(message) ⇒ Object



25
26
27
28
# File 'lib/logstash-logger/device/connectable.rb', line 25

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

#write_batch(messages) ⇒ Object



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

def write_batch(messages)
  with_connection do
    messages.each do |message|
      @io.write(message)
    end
  end
end