install our own ctrl-c handler, try to shut down guard and thin more gracefully

This commit is contained in:
Thomas Reynolds 2011-12-09 16:15:44 -08:00
parent b9e1d963aa
commit 789983c1ee

View file

@ -2,6 +2,7 @@ require "guard"
require "guard/guard" require "guard/guard"
require "rbconfig" require "rbconfig"
require "net/http" require "net/http"
require "thin"
if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw} if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw}
require "win32/process" require "win32/process"
@ -28,7 +29,7 @@ module Middleman
} }
::Guard.start({ ::Guard.start({
:guardfile_contents => guardfile_contents, :guardfile_contents => guardfile_contents,
:watch_all_modifications => true :watch_all_modifications => true
}) })
end end
@ -36,6 +37,13 @@ module Middleman
end end
end end
# Shut up Guard
module Guard::UI
class << self
def info(message, options = { }); end
end
end
# @private # @private
module Guard module Guard
class Middleman < Guard class Middleman < Guard
@ -45,12 +53,35 @@ module Guard
end end
def start def start
server_start @server_job = fork do
env = (@options[:environment] || "development").to_sym
is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
app = ::Middleman.server.inst do
set :environment, env
set :logging, is_logging
end
::Thin::Logging.silent = !is_logging
app_rack = app.class.to_rack_app
opts = @options.dup
opts[:app] = app_rack
puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
::Middleman.start_server(opts)
end
end
def stop
puts "== The Middleman is shutting down"
Process.kill("KILL", @server_job)
Process.wait @server_job
@server_job = nil
end end
def reload def reload
server_stop stop
server_start start
end end
def run_on_change(paths) def run_on_change(paths)
@ -79,35 +110,6 @@ module Guard
end end
private private
def server_start
# Quiet down Guard
# ENV['GUARD_ENV'] = 'test' if @options[:debug] == "true"
@server_job = fork do
env = (@options[:environment] || "development").to_sym
is_logging = @options.has_key?(:debug) && (@options[:debug] == "true")
app = ::Middleman.server.inst do
set :environment, env
set :logging, is_logging
end
app_rack = app.class.to_rack_app
opts = @options.dup
opts[:app] = app_rack
puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
::Middleman.start_server(opts)
end
end
def server_stop
puts "== The Middleman is shutting down"
Process.kill("KILL", @server_job)
Process.wait @server_job
@server_job = nil
# @app = nil
end
def talk_to_server(params={}) def talk_to_server(params={})
uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__") uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
Net::HTTP.post_form(uri, {}.merge(params)) Net::HTTP.post_form(uri, {}.merge(params))
@ -122,3 +124,8 @@ module Guard
end end
end end
end end
trap(:INT) do
::Guard.stop
exit
end