From f49d8918585b1de1bf74dbbe019bf388ec0581ec Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Tue, 19 May 2020 21:20:25 -0500 Subject: [PATCH 1/4] Filter only to create and write events on watching UI config Signed-off-by: Ruben Vargas --- cmd/query/app/static_handler.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index cde083e0bd5..14f0a3e20a5 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -149,16 +149,17 @@ func (sH *StaticAssetsHandler) watch() { } continue } + if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { + // 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)) - // 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)) + content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) + if err != nil { + sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) + } - content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) - if err != nil { - sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) + sH.indexHTML.Store(content) } - - sH.indexHTML.Store(content) case err, ok := <-watcher.Errors: if !ok { return From 33a921ca5946a31abf129e3e020486d5ee467518 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Wed, 20 May 2020 13:59:04 -0500 Subject: [PATCH 2/4] Refactor watcher handler Signed-off-by: Ruben Vargas --- cmd/query/app/static_handler.go | 65 ++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 14f0a3e20a5..472283931cf 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -126,6 +126,41 @@ func loadIndexBytes(open func(string) (http.File, error), options StaticAssetsHa return indexBytes, nil } +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 { + 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)) + + content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) + if err != nil { + sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) + } + + sH.indexHTML.Store(content) + } + 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 @@ -138,35 +173,7 @@ func (sH *StaticAssetsHandler) watch() { } 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 - } - if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create { - // 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)) - - content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) - if err != nil { - sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) - } - - sH.indexHTML.Store(content) - } - case err, ok := <-watcher.Errors: - if !ok { - return - } - sH.options.Logger.Error("event", zap.Error(err)) - } - } + sH.configListener(watcher) }() err = watcher.Add(sH.options.UIConfigPath) From 9cd1b4e9a6044e11131f34d1c207e4bc74896de6 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Thu, 21 May 2020 15:55:40 -0500 Subject: [PATCH 3/4] Address some observations Signed-off-by: Ruben Vargas --- cmd/query/app/static_handler.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index 472283931cf..d1a89ce4c98 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -130,28 +130,21 @@ 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 { - sH.options.Logger.Warn("the UI config file has been removed, using the last known version") - } + // Ignore if the event filename is not the UI configuration or if it is a chmod event. + if filepath.Base(event.Name) != filepath.Base(sH.options.UIConfigPath) || event.Op&fsnotify.Chmod == fsnotify.Chmod { continue } - if event.Op&fsnotify.Write != fsnotify.Write && event.Op&fsnotify.Create != fsnotify.Create { + if event.Op&fsnotify.Remove == fsnotify.Remove { + sH.options.Logger.Warn("the UI config file has been removed, using the last known version") 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)) - - content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) - if err != nil { - sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) - } - - sH.indexHTML.Store(content) + // 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)) + content, err := loadIndexBytes(sH.assetsFS.Open, sH.options) + if err != nil { + sH.options.Logger.Error("error while reloading the UI config", zap.Error(err)) } + sH.indexHTML.Store(content) case err, ok := <-watcher.Errors: if !ok { return From 0dad263e9de2b93d8c2ca79af5605bcc1b3164f1 Mon Sep 17 00:00:00 2001 From: Ruben Vargas Date: Thu, 21 May 2020 17:26:11 -0500 Subject: [PATCH 4/4] Separate name and event conditions on config watcher Signed-off-by: Ruben Vargas --- cmd/query/app/static_handler.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/query/app/static_handler.go b/cmd/query/app/static_handler.go index d1a89ce4c98..ceea5f243dd 100644 --- a/cmd/query/app/static_handler.go +++ b/cmd/query/app/static_handler.go @@ -130,8 +130,12 @@ func (sH *StaticAssetsHandler) configListener(watcher *fsnotify.Watcher) { for { select { case event := <-watcher.Events: - // Ignore if the event filename is not the UI configuration or if it is a chmod event. - if filepath.Base(event.Name) != filepath.Base(sH.options.UIConfigPath) || event.Op&fsnotify.Chmod == fsnotify.Chmod { + // ignore if the event filename is not the UI configuration + if filepath.Base(event.Name) != filepath.Base(sH.options.UIConfigPath) { + continue + } + // ignore if the event is a chmod event (permission or owner changes) + if event.Op&fsnotify.Chmod == fsnotify.Chmod { continue } if event.Op&fsnotify.Remove == fsnotify.Remove {