Gracefully handle syntax (and other) errors when reloading config.rb.

With this change, config.rb modifications that result in an error will print the error to the logs but will not kill the server - the previous version of the application will still be running just fine, so that you can try and fix your config.rb without having to restart. The server no longer simply hangs with no message, which was the previous behavior. This fixes #702.
This commit is contained in:
Ben Hollis 2013-10-19 19:39:10 -07:00
parent e773e8c1a9
commit 441ec95065
2 changed files with 33 additions and 12 deletions

View file

@ -0,0 +1,7 @@
helpers do
def title
content_tag :h1 do
"Title"
end
end
end

View file

@ -17,7 +17,7 @@ module Middleman
@host = @options[:host] || Socket.gethostname @host = @options[:host] || Socket.gethostname
@port = @options[:port] || DEFAULT_PORT @port = @options[:port] || DEFAULT_PORT
mount_instance mount_instance(new_app)
logger.info "== The Middleman is standing watch at http://#{host}:#{port}" logger.info "== The Middleman is standing watch at http://#{host}:#{port}"
logger.info "== Inspect your site configuration at http://#{host}:#{port}/__middleman/" logger.info "== Inspect your site configuration at http://#{host}:#{port}/__middleman/"
@ -31,12 +31,17 @@ module Middleman
# reloading later on. # reloading later on.
::Middleman::Profiling.report("server_start") ::Middleman::Profiling.report("server_start")
@webrick.start loop do
@webrick.start
# $mm_shutdown is set by the signal handler # $mm_shutdown is set by the signal handler
if $mm_shutdown if $mm_shutdown
shutdown shutdown
exit exit
elsif $mm_reload
$mm_reload = false
reload
end
end end
end end
end end
@ -62,10 +67,18 @@ module Middleman
def reload def reload
logger.info "== The Middleman is reloading" logger.info "== The Middleman is reloading"
unmount_instance begin
mount_instance app = new_app
rescue Exception => e
logger.error "Error reloading Middleman: #{e}\n#{e.backtrace.join("\n")}"
logger.info "== The Middleman is still running the application from before the error"
return
end
logger.info "== The Middleman is standing watch at http://#{host}:#{port}" unmount_instance
mount_instance(app)
logger.info "== The Middleman has reloaded"
end end
# Stop the current instance, exit Webrick # Stop the current instance, exit Webrick
@ -112,7 +125,8 @@ module Middleman
# See if the changed file is config.rb or lib/*.rb # See if the changed file is config.rb or lib/*.rb
if needs_to_reload?(added_and_modified + removed) if needs_to_reload?(added_and_modified + removed)
reload $mm_reload = true
@webrick.stop
else else
added_and_modified.each do |path| added_and_modified.each do |path|
app.files.did_change(path) app.files.did_change(path)
@ -168,8 +182,8 @@ module Middleman
# Attach a new Middleman::Application instance # Attach a new Middleman::Application instance
# @param [Middleman::Application] app # @param [Middleman::Application] app
# @return [void] # @return [void]
def mount_instance def mount_instance(app)
@app = new_app @app = app
@webrick ||= setup_webrick(@options[:debug] || false) @webrick ||= setup_webrick(@options[:debug] || false)