shithub: hugo

Download patch

ref: a655e00d702dbc20b3961b131b33ab21841b043d
parent: 9d973004f5379cff2adda489566fe40683553c4c
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Aug 16 08:30:03 EDT 2018

commands: Gracefully handle typos in server config when running the server

Fixes #5081

--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -71,6 +71,7 @@
 	languages           langs.Languages
 
 	configured bool
+	paused     bool
 }
 
 func (c *commandeer) Set(key string, value interface{}) {
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -646,13 +646,22 @@
 
 func (c *commandeer) fullRebuild() {
 	c.commandeerHugoState = &commandeerHugoState{}
-	if err := c.loadConfig(true, true); err != nil {
+	err := c.loadConfig(true, true)
+	if err != nil {
 		jww.ERROR.Println("Failed to reload config:", err)
-	} else if err := c.buildSites(); err != nil {
-		jww.ERROR.Println(err)
-	} else if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
-		livereload.ForceRefresh()
+		// Set the processing on pause until the state is recovered.
+		c.paused = true
+	} else {
+		c.paused = false
 	}
+
+	if !c.paused {
+		if err := c.buildSites(); err != nil {
+			jww.ERROR.Println(err)
+		} else if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
+			livereload.ForceRefresh()
+		}
+	}
 }
 
 // newWatcher creates a new watcher to watch filesystem events.
@@ -691,6 +700,23 @@
 		for {
 			select {
 			case evs := <-watcher.Events:
+				for _, ev := range evs {
+					if configSet[ev.Name] {
+						if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
+							continue
+						}
+						// Config file changed. Need full rebuild.
+						c.fullRebuild()
+						break
+					}
+				}
+
+				if c.paused {
+					// Wait for the server to get into a consistent state before
+					// we continue with processing.
+					continue
+				}
+
 				if len(evs) > 50 {
 					// This is probably a mass edit of the content dir.
 					// Schedule a full rebuild for when it slows down.
@@ -706,15 +732,6 @@
 				// Special handling for symbolic links inside /content.
 				filtered := []fsnotify.Event{}
 				for _, ev := range evs {
-					if configSet[ev.Name] {
-						if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
-							continue
-						}
-						// Config file changed. Need full rebuild.
-						c.fullRebuild()
-						break
-					}
-
 					// Check the most specific first, i.e. files.
 					contentMapped := c.hugo.ContentChanges.GetSymbolicLinkMappings(ev.Name)
 					if len(contentMapped) > 0 {