summaryrefslogtreecommitdiff
path: root/lib/chef_zero/server.rb
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-05-27 10:12:42 -0700
committerJohn Keiser <jkeiser@opscode.com>2014-05-27 10:12:42 -0700
commit4e80953c0c8eba632cbea441846534d4e2e0b8e9 (patch)
tree8399d05d64882d2e0922d1b62bc2762352bd31f2 /lib/chef_zero/server.rb
parentdf2f3b5bb176df112b7ece091be27736263fed61 (diff)
downloadchef-zero-4e80953c0c8eba632cbea441846534d4e2e0b8e9.tar.gz
Make sure the server is actually running after start()
Diffstat (limited to 'lib/chef_zero/server.rb')
-rw-r--r--lib/chef_zero/server.rb32
1 files changed, 18 insertions, 14 deletions
diff --git a/lib/chef_zero/server.rb b/lib/chef_zero/server.rb
index d354e87..3c8dfb6 100644
--- a/lib/chef_zero/server.rb
+++ b/lib/chef_zero/server.rb
@@ -188,12 +188,25 @@ module ChefZero
:BindAddress => @options[:host],
:Port => @options[:port],
:AccessLog => [],
- :Logger => WEBrick::Log.new(StringIO.new, 7)
+ :Logger => WEBrick::Log.new(StringIO.new, 7),
+ :StartCallback => proc {
+ @running = true
+ }
)
@server.mount('/', Rack::Handler::WEBrick, app)
- @thread = Thread.new { @server.start }
- @thread.abort_on_exception = true
+ @thread = Thread.new do
+ begin
+ Thread.abort_on_exception = true
+ @server.start
+ ensure
+ @running = false
+ end
+ end
+ # Do not return until the web server is genuinely started.
+ while !@running && @thread.alive?
+ sleep(0.01)
+ end
@thread
end
@@ -206,16 +219,7 @@ module ChefZero
# true if the server is accepting requests, false otherwise
#
def running?
- if @server.nil? || @server.status != :Running
- return false
- end
-
- uri = URI.join(url, 'cookbooks')
- headers = { 'Accept' => 'application/json' }
-
- Timeout.timeout(0.1) { !open(uri, headers).nil? }
- rescue SocketError, Errno::ECONNREFUSED, Timeout::Error
- false
+ !@server.nil? && @running && @server.status == :Running
end
#
@@ -226,7 +230,7 @@ module ChefZero
# server
#
def stop(wait = 5)
- if @thread
+ if @running
@server.shutdown
@thread.join(wait)
end