fix(os/gcfg): ignore fsnotify event error to avoid package gcfg totally failing (#4400)

问题描述: Windows 11
文件夹映射的网络驱动器里面的go项目在启动的时候会因为系统没有映射磁盘的文件事件监听而报错,从而导致整个项目启动失败,目前我临时的修复是将该错误改为警告进行打印

---------

Co-authored-by: anno <anno@anno.com>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: hailaz <739476267@qq.com>
This commit is contained in:
Aitimate
2025-11-21 22:51:42 +08:00
committed by GitHub
parent 99d69857fa
commit 54b7c249fd
2 changed files with 6 additions and 8 deletions

View File

@ -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

View File

@ -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.