install our own ctrl-c handler, try to shut down guard and thin more gracefully
This commit is contained in:
parent
b9e1d963aa
commit
789983c1ee
1 changed files with 40 additions and 33 deletions
|
@ -2,6 +2,7 @@ require "guard"
|
|||
require "guard/guard"
|
||||
require "rbconfig"
|
||||
require "net/http"
|
||||
require "thin"
|
||||
|
||||
if RbConfig::CONFIG['host_os'].downcase =~ %r{mingw}
|
||||
require "win32/process"
|
||||
|
@ -36,6 +37,13 @@ module Middleman
|
|||
end
|
||||
end
|
||||
|
||||
# Shut up Guard
|
||||
module Guard::UI
|
||||
class << self
|
||||
def info(message, options = { }); end
|
||||
end
|
||||
end
|
||||
|
||||
# @private
|
||||
module Guard
|
||||
class Middleman < Guard
|
||||
|
@ -45,12 +53,35 @@ module Guard
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
def reload
|
||||
server_stop
|
||||
server_start
|
||||
stop
|
||||
start
|
||||
end
|
||||
|
||||
def run_on_change(paths)
|
||||
|
@ -79,35 +110,6 @@ module Guard
|
|||
end
|
||||
|
||||
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={})
|
||||
uri = URI.parse("http://#{@options[:host]}:#{@options[:port]}/__middleman__")
|
||||
Net::HTTP.post_form(uri, {}.merge(params))
|
||||
|
@ -122,3 +124,8 @@ module Guard
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
trap(:INT) do
|
||||
::Guard.stop
|
||||
exit
|
||||
end
|
Loading…
Reference in a new issue