Class: Palmade::Tapsilog::Conn

Inherits:
Object
  • Object
show all
Defined in:
lib/palmade/tapsilog/conn.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, key, buffered = false) ⇒ Conn

Returns a new instance of Conn.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/palmade/tapsilog/conn.rb', line 6

def initialize(target, key, buffered = false)
  if target =~ /(.+)\:(\d+)$/i
    @host = $~[1]
    @port = $~[2]
  else
    @host = target
    @port = nil
  end
  @key = key
  @socket = nil
  @buffered = buffered
  @max_tries = 6
end

Instance Attribute Details

#max_triesObject

Returns the value of attribute max_tries.



4
5
6
# File 'lib/palmade/tapsilog/conn.rb', line 4

def max_tries
  @max_tries
end

#socketObject (readonly)

Returns the value of attribute socket.



3
4
5
# File 'lib/palmade/tapsilog/conn.rb', line 3

def socket
  @socket
end

Instance Method Details

#closeObject



67
68
69
# File 'lib/palmade/tapsilog/conn.rb', line 67

def close
  @socket.nil? ? nil : @socket.close rescue nil
end

#closed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/palmade/tapsilog/conn.rb', line 71

def closed?
  @socket.nil? ? true : @socket.closed?
end

#connect(host, port) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/palmade/tapsilog/conn.rb', line 20

def connect(host, port)
  if @socket.nil? || @socket.closed?
    real_connect(host, port)
    log('default', $$, 'authentication', @key)
  else
    @socket
  end
end

#flushObject



63
64
65
# File 'lib/palmade/tapsilog/conn.rb', line 63

def flush
  @socket.flush unless @socket.nil?
end

#log(service, instance_key, severity, message, tags = {}, ts = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/palmade/tapsilog/conn.rb', line 29

def log(service, instance_key, severity, message, tags = {}, ts = nil)
  tries = 0
  connect(@host, @port)

  ts = Time.now if ts.nil?
  tag_string = Utils.hash_to_query_string(tags)

  fullmsg = ":#{service}:#{instance_key}:#{severity}:#{tag_string}:#{message}"

  # Truncate below the 8192 limit on Tapsilog service
  fullmsg = fullmsg[0,8190] if fullmsg.size > 8190

  len = [fullmsg.length].pack('i')

  begin
    # first 8-bytes, is len and checksum
    write "#{len}#{len}#{fullmsg}"
  rescue Exception => e
    $stderr.puts "Failed to write to server! Retrying... (#{tries})" +
                 "#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n")}" unless @max_tries == -1

    if @max_tries == -1 || tries < @max_tries
      tries += 1
      close_possibly_dead_conn(tries)
      reconnect
      retry
    else
      raise e
    end
  end

  len
end

#reconnect!Object Also known as: reconnect



75
76
77
78
# File 'lib/palmade/tapsilog/conn.rb', line 75

def reconnect!
  close unless closed?
  connect(@host, @port)
end