commands: And now really fix the server watch logic

See #4275
This commit is contained in:
Bjørn Erik Pedersen 2018-01-15 10:02:17 +01:00
parent 4e524ffcff
commit d4f8f88e67
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 38 additions and 12 deletions

View file

@ -18,8 +18,10 @@ package commands
import (
"fmt"
"io/ioutil"
"os/signal"
"sort"
"sync/atomic"
"syscall"
"golang.org/x/sync/errgroup"
@ -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
@ -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
}
@ -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
@ -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)
@ -1183,7 +1207,7 @@ func (c *commandeer) newWatcher(dirList ...string) error {
}
}()
return nil
return watcher, nil
}
func pickOneWriteOrCreatePath(events []fsnotify.Event) string {

View file

@ -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
}
@ -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()