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

Ignore chmod events on UI config watcher. #2254

Merged
merged 4 commits into from
May 21, 2020
Merged
Changes from 2 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
64 changes: 36 additions & 28 deletions cmd/query/app/static_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,22 @@ func loadIndexBytes(open func(string) (http.File, error), options StaticAssetsHa
return indexBytes, nil
}

func (sH *StaticAssetsHandler) watch() {
if sH.options.UIConfigPath == "" {
return
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
sH.options.Logger.Error("failed to create a new watcher for the UI config", zap.Error(err))
return
}

go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Remove == fsnotify.Remove {
// this might be related to a file inside the dir, so, just log a warn if this is about the file we care about
// otherwise, just ignore the event
if event.Name == sH.options.UIConfigPath {
sH.options.Logger.Warn("the UI config file has been removed, using the last known version")
}
continue
func (sH *StaticAssetsHandler) configListener(watcher *fsnotify.Watcher) {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Remove == fsnotify.Remove {
// this might be related to a file inside the dir, so, just log a warn if this is about the file we care about
// otherwise, just ignore the event
if event.Name == sH.options.UIConfigPath {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct comparison. Based on the documentation of event.Name, it is the name of the file without the path, whereas UIConfigPath may contain the path.

You probably want filepath.Base(UIConfigPath)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, this check could be done first, before checking for event.Op.

Copy link
Contributor Author

@rubenvp8510 rubenvp8510 May 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation says relative path: https://godoc.org/github.com/fsnotify/fsnotify#Event

Anyway, for comparison proposes I get the base of both.

sH.options.Logger.Warn("the UI config file has been removed, using the last known version")
}

continue
}
if event.Op&fsnotify.Write != fsnotify.Write && event.Op&fsnotify.Create != fsnotify.Create {
continue
}
if event.Name == sH.options.UIConfigPath {
// this will catch events for all files inside the same directory, which is OK if we don't have many changes
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfigPath))

Expand All @@ -159,13 +151,29 @@ func (sH *StaticAssetsHandler) watch() {
}

sH.indexHTML.Store(content)
case err, ok := <-watcher.Errors:
if !ok {
return
}
sH.options.Logger.Error("event", zap.Error(err))
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
sH.options.Logger.Error("event", zap.Error(err))
}
}
}

func (sH *StaticAssetsHandler) watch() {
if sH.options.UIConfigPath == "" {
return
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
sH.options.Logger.Error("failed to create a new watcher for the UI config", zap.Error(err))
return
}

go func() {
sH.configListener(watcher)
}()

err = watcher.Add(sH.options.UIConfigPath)
Expand Down