Skip to content

Commit

Permalink
commands: Handle mass content etc. edits in server mode
Browse files Browse the repository at this point in the history
Fixes #4563
  • Loading branch information
bep committed Apr 4, 2018
1 parent 4f639d6 commit 730b66b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,7 @@
[[constraint]]
name = "github.com/sanity-io/litter"
version = "1.1.0"

[[constraint]]
branch = "master"
name = "github.com/bep/debounce"
17 changes: 16 additions & 1 deletion commands/commandeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"
"path/filepath"
"sync"
"time"

"github.com/spf13/cobra"

Expand All @@ -26,6 +27,7 @@ import (

"github.com/gohugoio/hugo/hugolib"

"github.com/bep/debounce"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/helpers"
Expand All @@ -51,6 +53,9 @@ type commandeer struct {
// We can do this only once.
fsCreate sync.Once

// Used in cases where we get flooded with events in server mode.
debounce func(f func())

serverPorts []int
languages helpers.Languages

Expand Down Expand Up @@ -90,10 +95,20 @@ func (c *commandeer) initFs(fs *hugofs.Fs) error {

func newCommandeer(running bool, doWithCommandeer func(c *commandeer) error, subCmdVs ...*cobra.Command) (*commandeer, error) {

var rebuildDebouncer func(f func())
if running {
// The time value used is tested with mass content replacements in a fairly big Hugo site.
// It is better to wait for some seconds in those cases rather than get flooded
// with rebuilds.
rebuildDebouncer, _ = debounce.New(4 * time.Second)
}

c := &commandeer{
doWithCommandeer: doWithCommandeer,
subCmdVs: append([]*cobra.Command{hugoCmdV}, subCmdVs...),
visitedURLs: types.NewEvictingStringQueue(10)}
visitedURLs: types.NewEvictingStringQueue(10),
debounce: rebuildDebouncer,
}

return c, c.loadConfig(running)
}
Expand Down
25 changes: 18 additions & 7 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,16 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
return Hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
}

func (c *commandeer) fullRebuild() {
if err := c.loadConfig(true); err != nil {
jww.ERROR.Println("Failed to reload config:", err)
} else if err := c.recreateAndBuildSites(true); err != nil {
jww.ERROR.Println(err)
} else if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
livereload.ForceRefresh()
}
}

// newWatcher creates a new watcher to watch filesystem events.
func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {
if runtime.GOOS == "darwin" {
Expand Down Expand Up @@ -887,6 +897,13 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {
for {
select {
case evs := <-watcher.Events:
if len(evs) > 50 {
// This is probably a mass edit of the content dir.
// Schedule a full rebuild for when it slows down.
c.debounce(c.fullRebuild)
continue
}

c.Logger.INFO.Println("Received System Events:", evs)

staticEvents := []fsnotify.Event{}
Expand All @@ -900,13 +917,7 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {
continue
}
// Config file changed. Need full rebuild.
if err := c.loadConfig(true); err != nil {
jww.ERROR.Println("Failed to reload config:", err)
} else if err := c.recreateAndBuildSites(true); err != nil {
jww.ERROR.Println(err)
} else if !buildWatch && !c.Cfg.GetBool("disableLiveReload") {
livereload.ForceRefresh()
}
c.fullRebuild()
break
}

Expand Down

0 comments on commit 730b66b

Please sign in to comment.