Improve server

This commit is contained in:
Jonas Nicklas 2009-11-07 15:35:47 +01:00
parent 351d5f2cf9
commit 00b75d8495
2 changed files with 21 additions and 18 deletions

View File

@ -8,7 +8,8 @@ module Webcat
attr_accessor :debug attr_accessor :debug
def log(message) def log(message)
puts message if debug puts "[webcat] #{message}" if debug
true
end end
end end

View File

@ -9,7 +9,7 @@ class Webcat::Server
end end
def port def port
9081 8080
end end
def host def host
@ -21,25 +21,17 @@ class Webcat::Server
end end
def boot 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 start_time = Time.now
Thread.new do Thread.new do
Rack::Handler::Mongrel.run @app, :Port => port Rack::Handler::Mongrel.run @app, :Port => port
end end
Webcat.log "[webcat] checking if application has booted" Webcat.log "checking if application has booted"
loop do loop do
begin Webcat.log("application has booted") and break if responsive?
res = Net::HTTP.start(host, port) { |http| http.get('/') } if Time.now - start_time > 10
Webcat.log "Rack application timed out during boot"
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"
exit exit
end end
@ -47,5 +39,15 @@ class Webcat::Server
sleep 1 sleep 1
end end
end end
end 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