lib/frankenstein/server.rb in frankenstein-1.2.0 vs lib/frankenstein/server.rb in frankenstein-2.0.0

- old
+ new

@@ -64,45 +64,39 @@ @op_mutex = Mutex.new @op_cv = ConditionVariable.new end - # Start the server instance running in a separate thread. + # Start the server instance running. # - # This method returns once the server is just about ready to start serving - # requests. + # This method will return only after `#shutdown` is called (presumably from + # another thread). # def run @op_mutex.synchronize do return AlreadyRunningError if @server - @server_thread = Thread.new do - @op_mutex.synchronize do - begin - wrapped_logger = Frankenstein::Server::WEBrickLogger.new(logger: @logger, progname: "Frankenstein::Server") - @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 - #:nocov: - @logger.fatal("Frankenstein::Server#run") { (["Exception while trying to create WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } - #:nocov: - ensure - @op_cv.signal - end - end - - begin - @server.start if @server - rescue => ex - #:nocov: - @logger.fatal("Frankenstein::Server#run") { (["Exception while running WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } - #:nocov: - end + begin + wrapped_logger = Frankenstein::Server::WEBrickLogger.new(logger: @logger, progname: "Frankenstein::Server") + @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 + #:nocov: + @logger.fatal("Frankenstein::Server#run") { (["Exception while trying to create WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } + #:nocov: + ensure + @op_cv.signal end end - @op_mutex.synchronize { @op_cv.wait(@op_mutex) until @server } + begin + @server.start if @server + rescue => ex + #:nocov: + @logger.fatal("Frankenstein::Server#run") { (["Exception while running WEBrick::HTTPServer: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") } + #:nocov: + end end # Terminate a running server instance. # # If the server isn't currently running, this call is a no-op. @@ -110,12 +104,10 @@ def shutdown @op_mutex.synchronize do return nil if @server.nil? @server.shutdown @server = nil - @server_thread.join - @server_thread = nil end end private @@ -123,12 +115,10 @@ @app ||= begin builder = Rack::Builder.new builder.use Rack::Deflater, if: ->(_, _, _, body) { body.any? && body[0].length > 512 } builder.use Prometheus::Middleware::Collector, registry: @registry, - metrics_prefix: @metrics_prefix, - counter_label_builder: ->(_, _) { {} }, - duration_label_builder: ->(_, _) { {} } + metrics_prefix: @metrics_prefix builder.use Prometheus::Middleware::Exporter, registry: @registry builder.run ->(_) { [301, { 'Location' => "/metrics", 'Content-Type' => 'text/plain' }, ["Try /metrics"]] } builder.to_app end end