运行文件系统监视器fsinotify以监视配置文件的更改 | GO

huangapple go评论85阅读模式
英文:

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 := &lt;-w.Errors:
			if !ok { 
				// Channel was closed (i.e. Watcher.Close() was called).
				return nil
			}
		// Read from Events.
		case e, ok := &lt;-w.Events:
			if !ok { 
				// Channel was closed (i.e. Watcher.Close() was called).
				return nil
			}

			if e.Has(fsnotify.Write) {
				fmt.Println(&quot;Something happen&quot;)
			}
		}
	}

   	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 (
	&quot;log&quot;

	&quot;github.com/fsnotify/fsnotify&quot;
)

func main() {
	// Creating a new watcher
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatalf(&quot;Failed to create watcher: %v&quot;, err)
	}

    //Defer close the watcher
	defer watcher.Close()

	// Adding some sample config file
	err = watcher.Add(&quot;config.json&quot;)
	if err != nil {
		log.Fatalf(&quot;Failed to add file to watcher: %v&quot;, err)
	}

	// Let&#39;s start the event loop
	for {
		select {
		case event := &lt;-watcher.Events:
			// Checks for any modification in config file
			if event.Op&amp;fsnotify.Write == fsnotify.Write {
				log.Println(&quot;Configuration file changed, reloading...&quot;)
				
			}
		case err := &lt;-watcher.Errors:
			log.Printf(&quot;Watcher error: %v&quot;, err)
		}
	}
}

huangapple
  • 本文由 发表于 2023年1月29日 18:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75274154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定