environment string
buildWatch bool
- poll bool
+ poll string
gc bool
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")
"syscall"
"time"
+ "github.com/gohugoio/hugo/common/types"
+
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/resources/page"
}
// 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()
}
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
}
"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 {
import (
"encoding/json"
"testing"
+ "time"
qt "github.com/frankban/quicktest"
)
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))
+
+}
--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)
--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
--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
--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)
"strings"
"time"
+ "github.com/gohugoio/hugo/common/types"
+
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/common/constants"
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
}
}