commands: And now really fix the server watch logic
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 15 Jan 2018 09:02:17 +0000 (10:02 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 15 Jan 2018 09:02:17 +0000 (10:02 +0100)
See #4275

commands/hugo.go
commands/server.go

index 67ebc8512ea7d721898701d9528c2b358dbbb962..dc90d48ecbd9f1f1985e9b4e7c3a091a21cc6eab 100644 (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 {
index b9f21783b6102e74a6f68785d1eacac4dfde03e2..0ae58c99163cc8ba254b2caf1ae1325f70c4cf58 100644 (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()