diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-02 09:54:03 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-07-04 16:12:28 +0200 |
commit | 24ce98b6d10b2088af61c15112f5c5ed915a0c35 (patch) | |
tree | 88b4090f4997b1763a1d9a49bcfa4ba92500647e /watcher/batcher.go | |
parent | 0019d60f67b6c4dde085753641a917fcd0aa4c76 (diff) |
Add polling as a fallback to native filesystem events in server watch
Fixes #8720
Fixes #6849
Fixes #7930
Diffstat (limited to 'watcher/batcher.go')
-rw-r--r-- | watcher/batcher.go | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/watcher/batcher.go b/watcher/batcher.go index 12c51940d..718eea73f 100644 --- a/watcher/batcher.go +++ b/watcher/batcher.go @@ -17,11 +17,12 @@ import ( "time" "github.com/fsnotify/fsnotify" + "github.com/gohugoio/hugo/watcher/filenotify" ) // Batcher batches file watch events in a given interval. type Batcher struct { - *fsnotify.Watcher + filenotify.FileWatcher interval time.Duration done chan struct{} @@ -29,12 +30,25 @@ type Batcher struct { } // New creates and starts a Batcher with the given time interval. -func New(interval time.Duration) (*Batcher, error) { - watcher, err := fsnotify.NewWatcher() +// It will fall back to a poll based watcher if native isn's supported. +// To always use polling, set poll to true. +func New(intervalBatcher, intervalPoll time.Duration, poll bool) (*Batcher, error) { + var err error + var watcher filenotify.FileWatcher + + if poll { + watcher = filenotify.NewPollingWatcher(intervalPoll) + } else { + watcher, err = filenotify.New(intervalPoll) + } + + if err != nil { + return nil, err + } batcher := &Batcher{} - batcher.Watcher = watcher - batcher.interval = interval + batcher.FileWatcher = watcher + batcher.interval = intervalBatcher batcher.done = make(chan struct{}, 1) batcher.Events = make(chan []fsnotify.Event, 1) @@ -42,7 +56,7 @@ func New(interval time.Duration) (*Batcher, error) { go batcher.run() } - return batcher, err + return batcher, nil } func (b *Batcher) run() { @@ -51,7 +65,7 @@ func (b *Batcher) run() { OuterLoop: for { select { - case ev := <-b.Watcher.Events: + case ev := <-b.FileWatcher.Events(): evs = append(evs, ev) case <-tick: if len(evs) == 0 { @@ -69,5 +83,5 @@ OuterLoop: // Close stops the watching of the files. func (b *Batcher) Close() { b.done <- struct{}{} - b.Watcher.Close() + b.FileWatcher.Close() } |