Class: GLO::LOG::Conn

Inherits:
Object
  • Object
show all
Defined in:
lib/glo-logging-server-client/conn.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, app) ⇒ Conn

Returns a new instance of Conn.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/glo-logging-server-client/conn.rb', line 4

def initialize ( target, app )

	if target =~ /(.+)\:(\d+)$/i
		@host = $~[1]
		@port = $~[2]
	else
		@host = target
		@port = nil
	end

	@app = app
	@max_tries = 6
	@socket = nil
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/glo-logging-server-client/conn.rb', line 45

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

#close_possibly_dead_conn(tries = 0) ⇒ Object



93
94
95
96
97
98
# File 'lib/glo-logging-server-client/conn.rb', line 93

def close_possibly_dead_conn( tries = 0)
	close unless @socket.nil? || closed?
	@socket = nil
	select(nil,nil,nil, tries * 0.2) if tries > 0
	@socket
end

#closed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/glo-logging-server-client/conn.rb', line 49

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

#conn_timeout(&block) ⇒ Object



100
101
102
# File 'lib/glo-logging-server-client/conn.rb', line 100

def conn_timeout( &block )
	::Timeout::timeout( 6, GLO::LOG::Timeout, &block )
end

#connect(host, port) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/glo-logging-server-client/conn.rb', line 60

def connect(host, port)
	if @socket.nil? || @socket.closed?
		real_connect(host, port)
	else
			@socket
	end
end

#flushObject



41
42
43
# File 'lib/glo-logging-server-client/conn.rb', line 41

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

#log(log_level = 'debug', msg = '', tag = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/glo-logging-server-client/conn.rb', line 19

def log ( log_level ='debug' , msg = '' , tag = {})
	tries = 0
	connect( @host, @port )
	data = {'app' => @app, 'log_level' => log_level,'message'=>msg,'tag'=>tag}.to_json
	data_size = data.size.to_s
	begin
		write "*2\r\n$3\r\nLOG\r\n$"+data_size+"\r\n"+data+"\r\n"
	rescue Exception => e
		$stderr.puts "Failed to write to server! Retrying... ( #{tries} )"

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

	end

end

#real_connect(host, port) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/glo-logging-server-client/conn.rb', line 68

def real_connect(host, port)
	tries = 0
	begin
		@socket = TCPSocket.new( host, port)
		raise "Unable to create socket!" if @socket.nil?
	rescue Exception => e
		$stderr.puts "Fauled to establish connection with server! Retrying..( #{tries} )" unless @max_tries == -1
		if @max_tries == -1 || tries < @max_tries
			tries += 1
			close_possibly_dead_conn(tries)
			retry
		else
			raise e
		end
	end
end

#reconnect!Object Also known as: reconnect



53
54
55
56
# File 'lib/glo-logging-server-client/conn.rb', line 53

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

#write(msg, flush = false) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/glo-logging-server-client/conn.rb', line 85

def write ( msg, flush = false )
	conn_timeout do
		wrtlen = @socket.write(msg)
	end
	self.flush if flush
	@socket
end