From 00b75d8495aa9a2da85a81a8985bcaa7f5241632 Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sat, 7 Nov 2009 15:35:47 +0100 Subject: [PATCH] Improve server --- lib/webcat.rb | 3 ++- lib/webcat/server.rb | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/webcat.rb b/lib/webcat.rb index 9bf5dc2e..a72b2d12 100644 --- a/lib/webcat.rb +++ b/lib/webcat.rb @@ -8,7 +8,8 @@ module Webcat attr_accessor :debug def log(message) - puts message if debug + puts "[webcat] #{message}" if debug + true end end diff --git a/lib/webcat/server.rb b/lib/webcat/server.rb index a5dbad78..4d634c98 100644 --- a/lib/webcat/server.rb +++ b/lib/webcat/server.rb @@ -9,7 +9,7 @@ class Webcat::Server end def port - 9081 + 8080 end def host @@ -21,25 +21,17 @@ class Webcat::Server end def boot - Webcat.log "[webcat] Booting Rack applicartion on port #{port}" + Webcat.log "application has already booted" and return if responsive? + Webcat.log "booting Rack applicartion on port #{port}" start_time = Time.now Thread.new do Rack::Handler::Mongrel.run @app, :Port => port end - Webcat.log "[webcat] checking if application has booted" + Webcat.log "checking if application has booted" loop do - begin - res = Net::HTTP.start(host, port) { |http| http.get('/') } - - if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection) - Webcat.log "[webcat] application has booted" - break - end - rescue Errno::ECONNREFUSED - end - - if Time.now - start_time > 5 - puts "[webcat] Rack application timed out during boot" + Webcat.log("application has booted") and break if responsive? + if Time.now - start_time > 10 + Webcat.log "Rack application timed out during boot" exit end @@ -47,5 +39,15 @@ class Webcat::Server sleep 1 end end - -end \ No newline at end of file + + def responsive? + res = Net::HTTP.start(host, port) { |http| http.get('/') } + + if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection) + return true + end + rescue Errno::ECONNREFUSED + return false + end + +end