From e31b1d194655ac3a38fe903ff3995806b129b88a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 5 Jul 2021 10:13:41 +0200 Subject: [PATCH] commands: Make the --poll flag a duration So you can do: ``` hugo server --poll 700ms ``` See #8720 --- commands/commands.go | 4 ++-- commands/hugo.go | 19 ++++++++++++++----- common/types/convert.go | 20 ++++++++++++++++++++ common/types/convert_test.go | 11 +++++++++++ docs/content/en/commands/hugo.md | 2 +- docs/content/en/commands/hugo_mod.md | 2 +- docs/content/en/commands/hugo_new.md | 2 +- docs/content/en/commands/hugo_server.md | 2 +- hugolib/site.go | 12 +++++------- 9 files changed, 56 insertions(+), 18 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 6aacc8e0..235f3591 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -204,7 +204,7 @@ type hugoBuilderCommon struct { environment string buildWatch bool - poll bool + poll string gc bool @@ -292,7 +292,7 @@ func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&cc.baseURL, "baseURL", "b", "", "hostname (and path) to the root, e.g. http://spf13.com/") cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date and author info to the pages") cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build") - cmd.Flags().BoolVar(&cc.poll, "poll", false, "use a poll based approach to watch for file system changes") + cmd.Flags().StringVar(&cc.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes") cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions") cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") diff --git a/commands/hugo.go b/commands/hugo.go index 7f3b4104..86aa8d57 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -30,6 +30,8 @@ import ( "syscall" "time" + "github.com/gohugoio/hugo/common/types" + "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/resources/page" @@ -820,7 +822,7 @@ func (c *commandeer) fullRebuild(changeType string) { } // newWatcher creates a new watcher to watch filesystem events. -func (c *commandeer) newWatcher(poll bool, dirList ...string) (*watcher.Batcher, error) { +func (c *commandeer) newWatcher(pollIntervalStr string, dirList ...string) (*watcher.Batcher, error) { if runtime.GOOS == "darwin" { tweakLimit() } @@ -830,10 +832,17 @@ func (c *commandeer) newWatcher(poll bool, dirList ...string) (*watcher.Batcher, return nil, err } - // The second interval is used by the poll based watcher. - // Setting a shorter interval would make it snappier, - // but it would consume more CPU. - watcher, err := watcher.New(500*time.Millisecond, 700*time.Millisecond, poll) + var pollInterval time.Duration + poll := pollIntervalStr != "" + if poll { + pollInterval, err = types.ToDurationE(pollIntervalStr) + if err != nil { + return nil, fmt.Errorf("invalid value for flag poll: %s", err) + } + c.logger.Printf("Use watcher with poll interval %v", pollInterval) + } + + watcher, err := watcher.New(500*time.Millisecond, pollInterval, poll) if err != nil { return nil, err } diff --git a/common/types/convert.go b/common/types/convert.go index 7beb3404..0560eda0 100644 --- a/common/types/convert.go +++ b/common/types/convert.go @@ -18,10 +18,30 @@ import ( "fmt" "html/template" "reflect" + "time" "github.com/spf13/cast" ) +// ToDuration converts v to time.Duration. +// See ToDurationE if you need to handle errors. +func ToDuration(v interface{}) time.Duration { + d, _ := ToDurationE(v) + return d +} + +// ToDurationE converts v to time.Duration. +func ToDurationE(v interface{}) (time.Duration, error) { + if n := cast.ToInt(v); n > 0 { + return time.Duration(n) * time.Millisecond, nil + } + d, err := time.ParseDuration(cast.ToString(v)) + if err != nil { + return 0, fmt.Errorf("cannot convert %v to time.Duration", v) + } + return d, nil +} + // ToStringSlicePreserveString is the same as ToStringSlicePreserveStringE, // but it never fails. func ToStringSlicePreserveString(v interface{}) []string { diff --git a/common/types/convert_test.go b/common/types/convert_test.go index 364228f4..88b30360 100644 --- a/common/types/convert_test.go +++ b/common/types/convert_test.go @@ -16,6 +16,7 @@ package types import ( "encoding/json" "testing" + "time" qt "github.com/frankban/quicktest" ) @@ -36,3 +37,13 @@ func TestToString(t *testing.T) { c.Assert(ToString([]byte("Hugo")), qt.Equals, "Hugo") c.Assert(ToString(json.RawMessage("Hugo")), qt.Equals, "Hugo") } + +func TestToDuration(t *testing.T) { + c := qt.New(t) + + c.Assert(ToDuration("200ms"), qt.Equals, 200*time.Millisecond) + c.Assert(ToDuration("200"), qt.Equals, 200*time.Millisecond) + c.Assert(ToDuration("4m"), qt.Equals, 4*time.Minute) + c.Assert(ToDuration("asdfadf"), qt.Equals, time.Duration(0)) + +} diff --git a/docs/content/en/commands/hugo.md b/docs/content/en/commands/hugo.md index f1e9c95c..43d9e77b 100644 --- a/docs/content/en/commands/hugo.md +++ b/docs/content/en/commands/hugo.md @@ -51,7 +51,7 @@ hugo [flags] --noChmod don't sync permission mode of files --noTimes don't sync modification time of files --path-warnings print warnings on duplicate target paths etc. - --poll use a poll based approach to watch for file system changes + --poll string set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes --print-mem print memory usage to screen at intervals --quiet build in quiet mode --renderToMemory render to memory (only useful for benchmark testing) diff --git a/docs/content/en/commands/hugo_mod.md b/docs/content/en/commands/hugo_mod.md index 70c765e9..044cff1a 100644 --- a/docs/content/en/commands/hugo_mod.md +++ b/docs/content/en/commands/hugo_mod.md @@ -46,7 +46,7 @@ See https://gohugo.io/hugo-modules/ for more information. --noChmod don't sync permission mode of files --noTimes don't sync modification time of files --path-warnings print warnings on duplicate target paths etc. - --poll use a poll based approach to watch for file system changes + --poll string set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes --print-mem print memory usage to screen at intervals --templateMetrics display metrics about template executions --templateMetricsHints calculate some improvement hints when combined with --templateMetrics diff --git a/docs/content/en/commands/hugo_new.md b/docs/content/en/commands/hugo_new.md index d6afe7ed..4c49d11c 100644 --- a/docs/content/en/commands/hugo_new.md +++ b/docs/content/en/commands/hugo_new.md @@ -47,7 +47,7 @@ hugo new [path] [flags] --noChmod don't sync permission mode of files --noTimes don't sync modification time of files --path-warnings print warnings on duplicate target paths etc. - --poll use a poll based approach to watch for file system changes + --poll string set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes --print-mem print memory usage to screen at intervals --templateMetrics display metrics about template executions --templateMetricsHints calculate some improvement hints when combined with --templateMetrics diff --git a/docs/content/en/commands/hugo_server.md b/docs/content/en/commands/hugo_server.md index 3c967582..d801054d 100644 --- a/docs/content/en/commands/hugo_server.md +++ b/docs/content/en/commands/hugo_server.md @@ -59,7 +59,7 @@ hugo server [flags] --noHTTPCache prevent HTTP caching --noTimes don't sync modification time of files --path-warnings print warnings on duplicate target paths etc. - --poll use a poll based approach to watch for file system changes + --poll string set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes -p, --port int port on which the server will listen (default 1313) --print-mem print memory usage to screen at intervals --renderToDisk render to Destination path (default is render to memory & serve from there) diff --git a/hugolib/site.go b/hugolib/site.go index 2e23368d..2e7e1d7f 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -29,6 +29,8 @@ import ( "strings" "time" + "github.com/gohugoio/hugo/common/types" + "github.com/gohugoio/hugo/common/paths" "github.com/gohugoio/hugo/common/constants" @@ -523,13 +525,9 @@ But this also means that your site configuration may not do what you expect. If timeout := 30 * time.Second if cfg.Language.IsSet("timeout") { v := cfg.Language.Get("timeout") - if n := cast.ToInt(v); n > 0 { - timeout = time.Duration(n) * time.Millisecond - } else { - d, err := time.ParseDuration(cast.ToString(v)) - if err == nil { - timeout = d - } + d, err := types.ToDurationE(v) + if err == nil { + timeout = d } } -- 2.30.2