lib/buildr/jetty.rb in buildr-0.22.0 vs lib/buildr/jetty.rb in buildr-1.0.0
- old
+ new
@@ -1,10 +1,11 @@
require "uri"
require "net/http"
require "core/project"
require "java/artifact"
require "java/java"
+require "thread"
module Buildr
# Provides a collection of tasks and methods for using Jetty, specifically as a server
# for testing your application.
@@ -67,20 +68,17 @@
# start(pipe?)
#
# Starts Jetty. This method does not return, it keeps the thread running until
# Jetty is stopped. If you want to run Jetty parallel with other tasks in the build,
# invoke the #use task instead.
- def start(pipe = nil)
+ def start(sync = nil)
begin
Java.rjb do
port = URI.parse(url).port
- puts "Starting Jetty at http://localhost:#{port}"
+ puts "Starting Jetty at http://localhost:#{port}" if verbose
jetty = Rjb::import("JettyWrapper").new(port)
- if pipe
- pipe.puts "Started"
- pipe.close
- end
+ sync << "Started" if sync
sleep # Forever
end
rescue Interrupt # Stopped from console
rescue Exception=>error
puts "#{error.class}: #{error.message}"
@@ -116,11 +114,11 @@
begin
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.request_get("/buildr/")
response.is_a?(Net::HTTPSuccess) && response.body =~ /Alive/
end
- rescue Errno::ECONNREFUSED
+ rescue Errno::ECONNREFUSED, Errno::EBADF
false
end
end
# :call-seq:
@@ -197,20 +195,15 @@
# It will spawn a separate process that will run Jetty, and will stop Jetty when
# the build ends. However, if you already started Jetty from the console (with
# take jetty:start), it will use the existing instance without shutting it down.
def fire()
unless running?
- reader, writer = IO.pipe
- if pid = fork
- # Wait for Jetty to fire up before doing anything else.
- if reader.gets == "Started"
- puts "Jetty started"
- reader.close
- end
- at_exit { stop }
- else
- start writer
- end
+ sync = Queue.new
+ Thread.new { start sync }
+ # Wait for Jetty to fire up before doing anything else.
+ sync.pop == "Started" or fail "Jetty not started"
+ puts "Jetty started" if verbose
+ at_exit { stop }
end
@setup.invoke
at_exit { @teardown.invoke }
end