Class: Onlylogs::SocketDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/onlylogs/socket_device.rb

Overview

Send failures are reported to $stderr — never through a logger — so a failing socket can never re-enter logging and deadlock or loop.

Constant Summary collapse

DEFAULT_SOCKET =
"tmp/sockets/onlylogs-sidecar.sock"

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: ENV.fetch("ONLYLOGS_SIDECAR_SOCKET", DEFAULT_SOCKET)) ⇒ SocketDevice

Returns a new instance of SocketDevice.



11
12
13
14
15
# File 'lib/onlylogs/socket_device.rb', line 11

def initialize(socket_path: ENV.fetch("ONLYLOGS_SIDECAR_SOCKET", DEFAULT_SOCKET))
  @socket_path = socket_path
  @socket_mutex = Mutex.new
  @socket = nil
end

Instance Method Details

#closeObject



30
31
32
# File 'lib/onlylogs/socket_device.rb', line 30

def close
  reconnect_socket
end

#write(message) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/onlylogs/socket_device.rb', line 17

def write(message)
  return if message.nil? || message.empty?

  socket = ensure_socket
  socket&.puts(message)
rescue Errno::EPIPE, Errno::ECONNREFUSED, Errno::ENOENT => e
  $stderr.puts "Onlylogs::SocketDevice error: #{e.message}" # rubocop:disable Style/StderrPuts
  reconnect_socket
rescue => e
  $stderr.puts "Onlylogs::SocketDevice unexpected error: #{e.class}: #{e.message}" # rubocop:disable Style/StderrPuts
  reconnect_socket
end