diff --git a/os/gcfg/gcfg_adapter_file.go b/os/gcfg/gcfg_adapter_file.go index 6c054896862..64be33238fd 100644 --- a/os/gcfg/gcfg_adapter_file.go +++ b/os/gcfg/gcfg_adapter_file.go @@ -297,7 +297,7 @@ func (a *AdapterFile) getJson(fileNameOrPath ...string) (configJson *gjson.Json, // Add monitor for this configuration file, // any changes of this file will refresh its cache in the Config object. if filePath != "" && !gres.Contains(filePath) { - _, err = gfsnotify.Add(filePath, func(event *gfsnotify.Event) { + _, err := gfsnotify.Add(filePath, func(event *gfsnotify.Event) { a.jsonMap.Remove(usedFileNameOrPath) if event.IsWrite() || event.IsRemove() || event.IsCreate() || event.IsRename() || event.IsChmod() { fileType := gfile.ExtName(usedFileNameOrPath) @@ -316,9 +316,10 @@ func (a *AdapterFile) getJson(fileNameOrPath ...string) (configJson *gjson.Json, } a.notifyWatchers(adapterCtx.Ctx) } + _ = event.Watcher.Remove(filePath) }) if err != nil { - return nil + intlog.Errorf(context.TODO(), "failed listen config file event[%s]: %v", filePath, err) } } return configJson diff --git a/os/gfsnotify/gfsnotify_watcher.go b/os/gfsnotify/gfsnotify_watcher.go index e475aea3c6e..9524d439dbc 100644 --- a/os/gfsnotify/gfsnotify_watcher.go +++ b/os/gfsnotify/gfsnotify_watcher.go @@ -20,8 +20,7 @@ import ( // The parameter `path` can be either a file or a directory path. // The optional parameter `recursive` specifies whether monitoring the `path` recursively, // which is true in default. -func (w *Watcher) Add( - path string, callbackFunc func(event *Event), option ...WatchOption, +func (w *Watcher) Add(path string, callbackFunc func(event *Event), option ...WatchOption, ) (callback *Callback, err error) { return w.AddOnce("", path, callbackFunc, option...) } @@ -35,8 +34,7 @@ func (w *Watcher) Add( // The parameter `path` can be either a file or a directory path. // The optional parameter `recursive` specifies whether monitoring the `path` recursively, // which is true in default. -func (w *Watcher) AddOnce( - name, path string, callbackFunc func(event *Event), option ...WatchOption, +func (w *Watcher) AddOnce(name, path string, callbackFunc func(event *Event), option ...WatchOption, ) (callback *Callback, err error) { var watchOption = w.getWatchOption(option...) w.nameSet.AddIfNotExistFuncLock(name, func() bool { @@ -89,8 +87,7 @@ func (w *Watcher) getWatchOption(option ...WatchOption) WatchOption { // addWithCallbackFunc adds the path to underlying monitor, creates and returns a callback object. // Very note that if it calls multiple times with the same `path`, the latest one will overwrite the previous one. -func (w *Watcher) addWithCallbackFunc( - name, path string, callbackFunc func(event *Event), option ...WatchOption, +func (w *Watcher) addWithCallbackFunc(name, path string, callbackFunc func(event *Event), option ...WatchOption, ) (callback *Callback, err error) { var watchOption = w.getWatchOption(option...) // Check and convert the given path to absolute path.