-
Notifications
You must be signed in to change notification settings - Fork 51
/
watcher.go
118 lines (98 loc) · 2.72 KB
/
watcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
// Package watcher implements a file watcher to update an object on file changes.
package watcher
import (
"fmt"
"log/slog"
"github.com/fsnotify/fsnotify"
)
// FileWatcher watches for changes to the file and calls the waiter's Update method.
type FileWatcher struct {
log *slog.Logger
updater updater
watcher eventWatcher
done chan struct{}
}
// New creates a new FileWatcher for the given validator.
func New(log *slog.Logger, updater updater) (*FileWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return &FileWatcher{
log: log,
watcher: &fsnotifyWatcher{watcher},
updater: updater,
done: make(chan struct{}, 1),
}, nil
}
// Close closes the watcher.
// It should only be called once.
func (f *FileWatcher) Close() error {
err := f.watcher.Close()
<-f.done
return err
}
// Watch starts watching the file at the given path.
// It will call the watcher's Update method when the file is modified.
func (f *FileWatcher) Watch(file string) error {
log := f.log.With("file", file)
defer func() { f.done <- struct{}{} }()
if err := f.watcher.Add(file); err != nil {
return err
}
for {
select {
case event, ok := <-f.watcher.Events():
if !ok {
log.Info("Watcher closed")
return nil
}
// file changes may be indicated by either a WRITE, CHMOD, CREATE or RENAME event
if event.Op&(fsnotify.Write|fsnotify.Chmod|fsnotify.Create|fsnotify.Rename) != 0 {
if err := f.updater.Update(); err != nil {
log.With(slog.Any("error", err)).Error("Update failed")
}
}
// if a file gets removed, e.g. by a rename event, we need to re-add the file to the watcher
if event.Has(fsnotify.Remove) {
if err := f.watcher.Add(event.Name); err != nil {
log.With(slog.Any("error", err)).Error("Failed to re-add file to watcher")
return fmt.Errorf("failed to re-add file %q to watcher: %w", event.Name, err)
}
}
case err := <-f.watcher.Errors():
if err != nil {
log.With(slog.Any("error", err)).Error("Watching for measurements updates")
return fmt.Errorf("watching for measurements updates: %w", err)
}
}
}
}
type updater interface {
Update() error
}
type eventWatcher interface {
Add(string) error
Close() error
Events() <-chan fsnotify.Event
Errors() <-chan error
}
type fsnotifyWatcher struct {
watcher *fsnotify.Watcher
}
func (w *fsnotifyWatcher) Add(file string) error {
return w.watcher.Add(file)
}
func (w *fsnotifyWatcher) Close() error {
return w.watcher.Close()
}
func (w *fsnotifyWatcher) Events() <-chan fsnotify.Event {
return w.watcher.Events
}
func (w *fsnotifyWatcher) Errors() <-chan error {
return w.watcher.Errors
}