Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove fsnotify #646

Merged
merged 2 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
name = "github.com/docker/distribution"
revision = "edc3ab29cdff8694dd6feb85cfeb4b5f1b38ed9c"

[[constraint]]
name = "github.com/fsnotify/fsnotify"
version = "1.4.7"

[[constraint]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.10.0"
Expand Down
107 changes: 12 additions & 95 deletions pkg/skaffold/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"sort"
"time"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

const quietPeriod = 500 * time.Millisecond

// WatcherFactory can build Watchers from a list of files to be watched for changes
type WatcherFactory func(paths []string) (Watcher, error)

Expand All @@ -42,20 +38,14 @@ type Watcher interface {
Start(ctx context.Context, out io.Writer, onChange func([]string) error) error
}

// fsWatcher uses inotify to watch for changes and implements
// the Watcher interface
type fsWatcher struct {
watcher *fsnotify.Watcher
files map[string]bool
}

// mtimeWatcher uses polling on file mTimes.
type mtimeWatcher struct {
files map[string]time.Time
}

func (m *mtimeWatcher) Start(ctx context.Context, out io.Writer, onChange func([]string) error) error {

c := time.NewTicker(2 * time.Second)
defer c.Stop()

changedPaths := map[string]bool{}

Expand Down Expand Up @@ -94,95 +84,22 @@ func (m *mtimeWatcher) Start(ctx context.Context, out io.Writer, onChange func([

// NewWatcher creates a new Watcher on a list of files.
func NewWatcher(paths []string) (Watcher, error) {
sort.Strings(paths)
logrus.Info("Starting mtime file watcher.")

// Get the watcher type to use, defaulting to mtime.
watcher := os.Getenv("SKAFFOLD_FILE_WATCHER")
if watcher == "" {
watcher = "mtime"
}
sort.Strings(paths)

switch watcher {
case "mtime":
logrus.Info("Starting mtime file watcher.")
files := map[string]time.Time{}
for _, p := range paths {
fi, err := os.Stat(p)
if err != nil {
return nil, err
}
files[p] = fi.ModTime()
}
return &mtimeWatcher{
files: files,
}, nil
case "fsnotify":
logrus.Info("Starting fsnotify file watcher.")
w, err := fsnotify.NewWatcher()
files := map[string]time.Time{}
for _, p := range paths {
fi, err := os.Stat(p)
if err != nil {
return nil, errors.Wrapf(err, "creating watcher")
}

files := map[string]bool{}

for _, p := range paths {
files[p] = true
logrus.Debugf("Added watch for %s", p)

if err := w.Add(p); err != nil {
w.Close()
return nil, errors.Wrapf(err, "adding watch for %s", p)
}

if err := w.Add(filepath.Dir(p)); err != nil {
w.Close()
return nil, errors.Wrapf(err, "adding watch for %s", p)
}
return nil, errors.Wrapf(err, "statting file %s", p)
}
return &fsWatcher{
watcher: w,
files: files,
}, nil
files[p] = fi.ModTime()
}
return nil, fmt.Errorf("unknown watch type: %s", watcher)
}

// Start watches a set of files for changes, and calls `onChange`
// on each file change.
func (f *fsWatcher) Start(ctx context.Context, out io.Writer, onChange func([]string) error) error {
changedPaths := map[string]bool{}

timer := time.NewTimer(1<<63 - 1) // Forever
defer timer.Stop()

for {
select {
case ev := <-f.watcher.Events:
if ev.Op == fsnotify.Chmod {
continue // TODO(dgageot): VSCode seems to chmod randomly
}
if !f.files[ev.Name] {
continue // File is not directly watched. Maybe its parent is
}
timer.Reset(quietPeriod)
logrus.Infof("Change: %s", ev)
changedPaths[ev.Name] = true
case err := <-f.watcher.Errors:
return errors.Wrap(err, "watch error")
case <-timer.C:
changes := sortedPaths(changedPaths)
changedPaths = map[string]bool{}

if err := onChange(changes); err != nil {
return errors.Wrap(err, "change callback")
}

fmt.Fprintln(out, "Watching for changes...")
case <-ctx.Done():
f.watcher.Close()
return nil
}
}
return &mtimeWatcher{
files: files,
}, nil
}

func sortedPaths(changedPaths map[string]bool) []string {
Expand Down
3 changes: 0 additions & 3 deletions pkg/skaffold/watch/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -63,8 +62,6 @@ func TestWatch(t *testing.T) {
for _, watcher := range watchers {
for _, test := range tests {
t.Run(fmt.Sprintf("%s %s", test.description, watcher), func(t *testing.T) {
os.Setenv("SKAFFOLD_FILE_WATCHER", watcher)
defer os.Setenv("SKAFFOLD_FILE_WATCHER", "")
tmp, teardown := testutil.TempDir(t)
defer teardown()

Expand Down
52 changes: 0 additions & 52 deletions vendor/github.com/fsnotify/fsnotify/AUTHORS

This file was deleted.

28 changes: 0 additions & 28 deletions vendor/github.com/fsnotify/fsnotify/LICENSE

This file was deleted.

37 changes: 0 additions & 37 deletions vendor/github.com/fsnotify/fsnotify/fen.go

This file was deleted.

66 changes: 0 additions & 66 deletions vendor/github.com/fsnotify/fsnotify/fsnotify.go

This file was deleted.

Loading