Shutdown even if the user closed their terminal. Also handle signals on Windows (and elsewhere) if available without resorting to platform detection. Fixes #581.

This commit is contained in:
Ben Hollis 2012-09-27 23:02:59 -07:00
parent 9efa9d8009
commit d0c00884fe

View file

@ -1,9 +1,6 @@
require "webrick" require "webrick"
module Middleman module Middleman
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
module PreviewServer module PreviewServer
DEFAULT_PORT = 4567 DEFAULT_PORT = 4567
@ -25,7 +22,7 @@ module Middleman
unless @initialized unless @initialized
@initialized = true @initialized = true
register_signal_handlers unless ::Middleman::WINDOWS register_signal_handlers
# Save the last-used @options so it may be re-used when # Save the last-used @options so it may be re-used when
# reloading later on. # reloading later on.
@ -38,7 +35,12 @@ module Middleman
# Detach the current Middleman::Application instance # Detach the current Middleman::Application instance
# @return [void] # @return [void]
def stop def stop
logger.info "== The Middleman is shutting down" begin
logger.info "== The Middleman is shutting down"
rescue
# if the user closed their terminal STDOUT/STDERR won't exist
end
if @listener if @listener
@listener.stop @listener.stop
@listener = nil @listener = nil
@ -108,12 +110,17 @@ module Middleman
@listener.start(false) if first_run @listener.start(false) if first_run
end end
# Trap the interupt signal and shut down smoothly # Trap some interupt signals and shut down smoothly
# @return [void] # @return [void]
def register_signal_handlers def register_signal_handlers
trap("INT") { shutdown } %w(INT HUP TERM QUIT).each do |sig|
trap("TERM") { shutdown } if Signal.list[sig]
trap("QUIT") { shutdown } Signal.trap(sig) do
shutdown
exit
end
end
end
end end
# Initialize webrick # Initialize webrick
@ -145,12 +152,13 @@ module Middleman
# @param [Middleman::Application] app # @param [Middleman::Application] app
# @return [void] # @return [void]
def mount_instance def mount_instance
@app = new_app
@webrick ||= setup_webrick( @webrick ||= setup_webrick(
@options[:host] || "0.0.0.0", @options[:host] || "0.0.0.0",
@options[:debug] || false @options[:debug] || false
) )
@app = new_app
start_file_watcher start_file_watcher
@webrick.mount "/", ::Rack::Handler::WEBrick, app.class.to_rack_app @webrick.mount "/", ::Rack::Handler::WEBrick, app.class.to_rack_app