From 441ec95065c89b31b3d0840e8dab56521546a167 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 19 Oct 2013 19:39:10 -0700 Subject: [PATCH] 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. --- .../fixtures/padrino-helpers-app/config.rb | 7 ++++ .../lib/middleman-core/preview_server.rb | 38 +++++++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/middleman-core/fixtures/padrino-helpers-app/config.rb b/middleman-core/fixtures/padrino-helpers-app/config.rb index e69de29b..182438ad 100644 --- a/middleman-core/fixtures/padrino-helpers-app/config.rb +++ b/middleman-core/fixtures/padrino-helpers-app/config.rb @@ -0,0 +1,7 @@ +helpers do + def title + content_tag :h1 do + "Title" + end + end +end diff --git a/middleman-core/lib/middleman-core/preview_server.rb b/middleman-core/lib/middleman-core/preview_server.rb index 34c55cb6..f94eeec3 100644 --- a/middleman-core/lib/middleman-core/preview_server.rb +++ b/middleman-core/lib/middleman-core/preview_server.rb @@ -17,7 +17,7 @@ module Middleman @host = @options[:host] || Socket.gethostname @port = @options[:port] || DEFAULT_PORT - mount_instance + mount_instance(new_app) logger.info "== The Middleman is standing watch at http://#{host}:#{port}" logger.info "== Inspect your site configuration at http://#{host}:#{port}/__middleman/" @@ -31,12 +31,17 @@ module Middleman # reloading later on. ::Middleman::Profiling.report("server_start") - @webrick.start + loop do + @webrick.start - # $mm_shutdown is set by the signal handler - if $mm_shutdown - shutdown - exit + # $mm_shutdown is set by the signal handler + if $mm_shutdown + shutdown + exit + elsif $mm_reload + $mm_reload = false + reload + end end end end @@ -62,10 +67,18 @@ module Middleman def reload logger.info "== The Middleman is reloading" - unmount_instance - mount_instance + begin + 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 # Stop the current instance, exit Webrick @@ -112,7 +125,8 @@ module Middleman # See if the changed file is config.rb or lib/*.rb if needs_to_reload?(added_and_modified + removed) - reload + $mm_reload = true + @webrick.stop else added_and_modified.each do |path| app.files.did_change(path) @@ -168,8 +182,8 @@ module Middleman # Attach a new Middleman::Application instance # @param [Middleman::Application] app # @return [void] - def mount_instance - @app = new_app + def mount_instance(app) + @app = app @webrick ||= setup_webrick(@options[:debug] || false)