Class: Le::Host::HTTP
Instance Attribute Summary collapse
Instance Method Summary
collapse
#format_message, #formatter
Constructor Details
#initialize(token, local, debug) ⇒ HTTP
Returns a new instance of HTTP.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/le/host/http.rb', line 12
def initialize(token, local, debug)
if defined?(Rails)
@logger_console = Logger.new("log/#{Rails.env}.log")
else
@logger_console = Logger.new(STDOUT)
end
@token = token
@local = local
@debug = debug
@queue = Queue.new
@started = false
@thread = nil
if @debug then
self.init_debug
end
end
|
Instance Attribute Details
#conn ⇒ Object
Returns the value of attribute conn.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def conn
@conn
end
|
#debug ⇒ Object
Returns the value of attribute debug.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def debug
@debug
end
|
#local ⇒ Object
Returns the value of attribute local.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def local
@local
end
|
#queue ⇒ Object
Returns the value of attribute queue.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def queue
@queue
end
|
#started ⇒ Object
Returns the value of attribute started.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def started
@started
end
|
#thread ⇒ Object
Returns the value of attribute thread.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def thread
@thread
end
|
#token ⇒ Object
Returns the value of attribute token.
10
11
12
|
# File 'lib/le/host/http.rb', line 10
def token
@token
end
|
Instance Method Details
#check_async_thread ⇒ Object
64
65
66
67
68
69
|
# File 'lib/le/host/http.rb', line 64
def check_async_thread
if not @thread.alive?
@thread = Thread.new{run()}
dbg "LE: Asyncrhonous socket writer restarted"
end
end
|
#close ⇒ Object
71
72
73
74
|
# File 'lib/le/host/http.rb', line 71
def close
dbg "LE: Closing asynchronous socket writer"
@started = false
end
|
#closeConnection ⇒ Object
103
104
105
106
107
108
|
# File 'lib/le/host/http.rb', line 103
def closeConnection
if @conn != nil
@conn.sysclose
@conn = nil
end
end
|
#dbg(message) ⇒ Object
38
39
40
41
42
|
# File 'lib/le/host/http.rb', line 38
def dbg(message)
if @debug then
@debug_logger.add(Logger::Severity::DEBUG,message)
end
end
|
#init_debug ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/le/host/http.rb', line 30
def init_debug
filePath = "logentriesGem.log"
if File.exist?('log/')
filePath = "log/logentriesGem.log"
end
@debug_logger = Logger.new(filePath)
end
|
#openConnection ⇒ Object
76
77
78
79
80
81
|
# File 'lib/le/host/http.rb', line 76
def openConnection
dbg "LE: Reopening connection to Logentries API server"
@conn = TCPSocket.new('api.logentries.com', 10000)
dbg "LE: Connection established"
end
|
#reopenConnection ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/le/host/http.rb', line 83
def reopenConnection
closeConnection
root_delay = 0.1
while true
begin
openConnection
break
rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e
dbg "LE: Unable to connect to Logentries"
end
root_delay *= 2
if root_delay >= 10 then
root_delay = 10
end
wait_for = (root_delay + rand(root_delay)).to_i
dbg "LE: Waiting for " + wait_for.to_s + "ms"
sleep(wait_for)
end
end
|
#run ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/le/host/http.rb', line 110
def run
reopenConnection
while true
data = @queue.pop
while true
begin
@conn.write(data)
rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEOUT, EOFError => e
reopenConnection
next
end
break
end
end
dbg "LE: Closing Asyncrhonous socket writer"
closeConnection
end
|
#start_async_thread ⇒ Object
58
59
60
61
62
|
# File 'lib/le/host/http.rb', line 58
def start_async_thread
@thread = Thread.new{run()}
dbg "LE: Asynchronous socket writer started"
@started = true
end
|
#write(message) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/le/host/http.rb', line 44
def write(message)
if @local then
@logger_console.add(Logger::Severity::UNKNOWN,message)
end
@queue << "#{@token}#{message}\n"
if @started then
check_async_thread
else
start_async_thread
end
end
|