Skip to content

Commit

Permalink
commands: And now really fix the server watch logic
Browse files Browse the repository at this point in the history
See #4275
  • Loading branch information
bep committed Jan 15, 2018
1 parent 4e524ff commit d4f8f88
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
44 changes: 34 additions & 10 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package commands
import (
"fmt"
"io/ioutil"
"os/signal"
"sort"
"sync/atomic"
"syscall"

"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -547,7 +549,7 @@ func (c *commandeer) watchConfig() {
})
}

func (c *commandeer) fullBuild(watches ...bool) error {
func (c *commandeer) fullBuild() error {
var (
g errgroup.Group
langCount map[string]uint64
Expand Down Expand Up @@ -611,10 +613,10 @@ func (c *commandeer) fullBuild(watches ...bool) error {

}

func (c *commandeer) build(watches ...bool) error {
func (c *commandeer) build() error {
defer c.timeTrack(time.Now(), "Total")

if err := c.fullBuild(watches...); err != nil {
if err := c.fullBuild(); err != nil {
return err
}

Expand All @@ -632,7 +634,31 @@ func (c *commandeer) build(watches ...bool) error {
}
c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")))
c.Logger.FEEDBACK.Println("Press Ctrl+C to stop")
utils.CheckErr(c.Logger, c.newWatcher(watchDirs...))
watcher, err := c.newWatcher(watchDirs...)
utils.CheckErr(c.Logger, err)
defer watcher.Close()

var sigs = make(chan os.Signal)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

<-sigs
}

return nil
}

func (c *commandeer) serverBuild() error {
defer c.timeTrack(time.Now(), "Total")

if err := c.fullBuild(); err != nil {
return err
}

// TODO(bep) Feedback?
if !quiet {
fmt.Println()
Hugo.PrintProcessingStats(os.Stdout)
fmt.Println()
}

return nil
Expand Down Expand Up @@ -968,24 +994,22 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
}

// newWatcher creates a new watcher to watch filesystem events.
func (c *commandeer) newWatcher(dirList ...string) error {
func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) {
if runtime.GOOS == "darwin" {
tweakLimit()
}

staticSyncer, err := newStaticSyncer(c)
if err != nil {
return err
return nil, err
}

watcher, err := watcher.New(1 * time.Second)

if err != nil {
return err
return nil, err
}

defer watcher.Close()

for _, d := range dirList {
if d != "" {
_ = watcher.Add(d)
Expand Down Expand Up @@ -1183,7 +1207,7 @@ func (c *commandeer) newWatcher(dirList ...string) error {
}
}()

return nil
return watcher, nil
}

func pickOneWriteOrCreatePath(events []fsnotify.Event) string {
Expand Down
6 changes: 4 additions & 2 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func server(cmd *cobra.Command, args []string) error {
return err
}

if err := c.build(serverWatch); err != nil {
if err := c.serverBuild(); err != nil {
return err
}

Expand Down Expand Up @@ -231,12 +231,14 @@ func server(cmd *cobra.Command, args []string) error {
rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(relWatchDirs)), ",")

jww.FEEDBACK.Printf("Watching for changes in %s%s{%s}\n", baseWatchDir, helpers.FilePathSeparator, rootWatchDirs)
err = c.newWatcher(watchDirs...)
watcher, err := c.newWatcher(watchDirs...)

if err != nil {
return err
}

defer watcher.Close()

}

return c.serve()
Expand Down

0 comments on commit d4f8f88

Please sign in to comment.