英文:
Run watcher fsinotify to watch configuration file changing | GO
问题
我正在尝试使用fsnotify库在Go语言中运行一个监视器,以跟踪配置JSON文件的更改。请使用以下代码:
func waitForChangeEventLoop(w *fsnotify.Watcher) error {
for {
select {
// 从Errors通道读取。
case _, ok := <-w.Errors:
if !ok {
// 通道已关闭(即调用了Watcher.Close())。
return nil
}
// 从Events通道读取。
case e, ok := <-w.Events:
if !ok {
// 通道已关闭(即调用了Watcher.Close())。
return nil
}
if e.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("发生了一些变化")
}
}
}
return nil
}
func setFileWatcher() error {
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go waitForChangeEventLoop(w)
err = w.Add(gConfiguredCollectorsPath)
if err != nil {
return err
}
defer w.Close()
return nil
}
在运行过程中,只要更改了JSON文件,您就会收到注释。
英文:
I am trying to run a watcher in go using fsinotfiy lib to follow configuration json file changes - use this code
func waitForChangeEventLoop(w *fsnotify.Watcher) (error) {
for {
select {
// Read from Errors.
case _, ok := <-w.Errors:
if !ok {
// Channel was closed (i.e. Watcher.Close() was called).
return nil
}
// Read from Events.
case e, ok := <-w.Events:
if !ok {
// Channel was closed (i.e. Watcher.Close() was called).
return nil
}
if e.Has(fsnotify.Write) {
fmt.Println("Something happen")
}
}
}
return nil
}
func setFileWatcher() (error) {
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
go waitForChangeEventLoop(w)
err = w.Add(gConfiguredCollectorsPath)
if err != nil {
return err
}
defer w.Close()
return nil
}
I am expecting to get notes during the process is running, any time I change the json file
答案1
得分: 1
这样应该可以工作。为了保持简单,我将其放在主函数中,但是将其移动到一个单独的方法中也可以工作。
package main
import (
"log"
"github.com/fsnotify/fsnotify"
)
func main() {
// 创建一个新的 watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("创建 watcher 失败:%v", err)
}
// 延迟关闭 watcher
defer watcher.Close()
// 添加一些示例配置文件
err = watcher.Add("config.json")
if err != nil {
log.Fatalf("将文件添加到 watcher 失败:%v", err)
}
// 开始事件循环
for {
select {
case event := <-watcher.Events:
// 检查配置文件是否有修改
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("配置文件已更改,重新加载...")
}
case err := <-watcher.Errors:
log.Printf("Watcher 错误:%v", err)
}
}
}
希望对你有帮助!
英文:
Something like this should work. Just doing it in main for keeping it simple but moving away in a separate method like you do should work too.
package main
import (
"log"
"github.com/fsnotify/fsnotify"
)
func main() {
// Creating a new watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Failed to create watcher: %v", err)
}
//Defer close the watcher
defer watcher.Close()
// Adding some sample config file
err = watcher.Add("config.json")
if err != nil {
log.Fatalf("Failed to add file to watcher: %v", err)
}
// Let's start the event loop
for {
select {
case event := <-watcher.Events:
// Checks for any modification in config file
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Configuration file changed, reloading...")
}
case err := <-watcher.Errors:
log.Printf("Watcher error: %v", err)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论