diff options
author | Bjørn Erik Pedersen <[email protected]> | 2018-01-15 10:02:17 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2018-01-15 10:02:17 +0100 |
commit | d4f8f88e67f958b8010f90cb9b9854114e52dac2 (patch) | |
tree | 72f83ad69b3f96da7d3047016e28d16e3c58be9d | |
parent | 4e524ffcfff48c017717e261c6067416aa56410f (diff) | |
download | hugo-d4f8f88e67f958b8010f90cb9b9854114e52dac2.tar.gz hugo-d4f8f88e67f958b8010f90cb9b9854114e52dac2.zip |
commands: And now really fix the server watch logic
See #4275
-rw-r--r-- | commands/hugo.go | 44 | ||||
-rw-r--r-- | commands/server.go | 6 |
2 files changed, 38 insertions, 12 deletions
diff --git a/commands/hugo.go b/commands/hugo.go index 67ebc8512..dc90d48ec 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -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 { diff --git a/commands/server.go b/commands/server.go index b9f21783b..0ae58c991 100644 --- a/commands/server.go +++ b/commands/server.go @@ -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() |