./lib/le/host/http.rb in le-2.1.2 vs ./lib/le/host/http.rb in le-2.1.3

- old
+ new

@@ -10,52 +10,63 @@ attr_accessor :token, :queue, :started, :thread, :conn, :local def initialize(token, local) @token = token @local = local - @queue = Queue.new + @queue = Queue.new @started = false + @thread = nil end def write(message) if @local then puts message return end @queue << "#{@token}#{message}\n" - if not @started then - puts "LE: Starting asynchronous socket writer" - @thread = Thread.new{run()} - @started = true + if @started then + check_async_thread + else + start_async_thread end end + def start_async_thread + @thread = Thread.new{run()} + puts "LE: Asynchronous socket writer started" + @started = true + end + + def check_async_thread + if not @thread.alive? + @thread = Thread.new{run()} + puts "LE: Asyncrhonous socket writer restarted" + end + end + def close puts "LE: Closing asynchronous socket writer" - @thread.raise Interrupt + @started = false end def openConnection puts "LE: Reopening connection to Logentries API server" @conn = TCPSocket.new('api.logentries.com', 10000) - #@conn = OpenSSL::SSL::SSLSocket.new(@sock, OpenSSL::SSL::SSLContext.new()) - #@conn.connect - puts "LE: Connection established" end def reopenConnection closeConnection root_delay = 0.1 while true begin openConnection break - rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e + rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError => e puts "LE: Unable to connect to Logentries" end root_delay *= 2 if root_delay >= 10 then root_delay = 10 @@ -69,34 +80,27 @@ def closeConnection if @conn != nil @conn.sysclose @conn = nil end - #if @sock != nil - # @sock.close - # @sock = nil - #end end def run - begin reopenConnection + while true + data = @queue.pop while true - data = @queue.pop - while true - begin - @conn.write(data) - rescue OpenSSL::SSL::SSLError, TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEOUT, EOFError => e - reopenConnection - next - end - break + begin + @conn.write(data) + rescue TimeoutError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ETIMEOUT, EOFError => e + reopenConnection + next end + break end - rescue Interrupt - puts "LE: Asynchronous socket writer interrupted" end + puts "LE: Closing Asyncrhonous socket writer" closeConnection end end end end