lib/frankenstein/server.rb in frankenstein-0.1.1 vs lib/frankenstein/server.rb in frankenstein-0.2.0

- old
+ new

@@ -6,10 +6,11 @@ require 'rack/builder' require 'rack/handler/webrick' require 'rack/deflater' require 'frankenstein/error' +require 'frankenstein/server/webrick_logger' module Frankenstein # A straightforward Prometheus metrics server. # # When you're looking to instrument your application which isn't, itself, a @@ -21,11 +22,11 @@ # # stats_server = Frankenstein::Server.new # stats_server.run # We are now serving stats on port 8080! # counter = stats_server.registry.counter(:seconds_count, "Number of seconds") # # Give the counter something to count - # loop { counter.increment({}) } + # loop { sleep 1; counter.increment({}) } # # Now if you hit http://localhost:8080/metrics you should see a counter # gradually going up, along with stats about the Frankenstein HTTP server # itself. Neato! # @@ -63,25 +64,36 @@ @op_mutex = Mutex.new @op_cv = ConditionVariable.new end - # Start the server instance running. + # Start the server instance running in a separate thread. # # This method returns once the server is just about ready to start serving # requests. # def run @op_mutex.synchronize do return AlreadyRunningError if @server @server_thread = Thread.new do @op_mutex.synchronize do - @server = WEBrick::HTTPServer.new(Logger: @logger, BindAddress: nil, Port: @port) - @server.mount "/", Rack::Handler::WEBrick, app - @op_cv.signal + begin + wrapped_logger = Frankenstein::Server::WEBrickLogger.new(logger: @logger) + @server = WEBrick::HTTPServer.new(Logger: wrapped_logger, BindAddress: nil, Port: @port, AccessLog: [[wrapped_logger, WEBrick::AccessLog::COMMON_LOG_FORMAT]]) + @server.mount "/", Rack::Handler::WEBrick, app + rescue => ex + @logger.fatal("Frankenstein::Server#run") { (["Exception while trying to create WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } + ensure + @op_cv.signal + end end - @server.start + + begin + @server.start if @server + rescue => ex + @logger.fatal("Frankenstein::Server#run") { (["Exception while running WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } + end end end @op_mutex.synchronize { @op_cv.wait(@op_mutex) until @server } end