1 Star 0 Fork 0

GoAdmin / admin-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
watcher_linux.go 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
GoAdmin 提交于 2022-06-08 16:39 . test
//+build linux
package file
import (
"os"
"github.com/fsnotify/fsnotify"
"github.com/go-admin-team/go-admin-core/config/source"
)
type watcher struct {
f *file
fw *fsnotify.Watcher
exit chan bool
}
func newWatcher(f *file) (source.Watcher, error) {
fw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
fw.Add(f.path)
return &watcher{
f: f,
fw: fw,
exit: make(chan bool),
}, nil
}
func (w *watcher) Next() (*source.ChangeSet, error) {
// is it closed?
select {
case <-w.exit:
return nil, source.ErrWatcherStopped
default:
}
// try get the event
select {
case event, _ := <-w.fw.Events:
if event.Op == fsnotify.Rename {
// check existence of file, and add watch again
_, err := os.Stat(event.Name)
if err == nil || os.IsExist(err) {
w.fw.Add(event.Name)
}
}
c, err := w.f.Read()
if err != nil {
return nil, err
}
// add path again for the event bug of fsnotify
w.fw.Add(w.f.path)
return c, nil
case err := <-w.fw.Errors:
return nil, err
case <-w.exit:
return nil, source.ErrWatcherStopped
}
}
func (w *watcher) Stop() error {
return w.fw.Close()
}
Go
1
https://gitee.com/GoAdminCore/admin-core.git
git@gitee.com:GoAdminCore/admin-core.git
GoAdminCore
admin-core
admin-core
v1.0.0

搜索帮助