-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive_monitor.go
252 lines (224 loc) · 6.75 KB
/
recursive_monitor.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package piaas
import (
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/sohoffice/piaas/util"
"hash/fnv"
"os"
"path"
"path/filepath"
"strings"
)
type MonitorPath struct {
// the path relative to project root
path string
}
type RecursiveMonitor struct {
basedir string
// This channel publish collected changes.
// The changes will be published to CollectObservers.
collectsCh chan []string
// The caller of RecursiveMonitor should use this to subscribe to collected changes.
collectObservers []chan<- []string
// Monitors currently being watched
monitors map[uint32]MonitorPath
// Receive file system changes from fsnotify
watcher *fsnotify.Watcher
// This channel will publish the observed file changes.
// Usually the channel receives file or directory name
// However, it may also receive an empty string which means all accumulated changes should be collected
changes chan string
// The observers that subscribe to changes event.
// Should only be used in testing
changesObservers []chan<- string
// The debouncer can be used to collect file change events over a small period of time.
debouncer util.Debouncer
// accumulated changes waiting to be collected.
//
// All values will be sent to collects channel when debounce event is triggered.
// `accumulated` will be cleared thereafter.
accumulated util.StringSet
}
// Start watching on all registered monitors, and manage the removal and addition of new directories.
func (rm *RecursiveMonitor) Start(debounceTime uint64) {
rm.debouncer = util.NewDebouncer(debounceTime, func(tag string) {
log.Debugf("Received debounce event: %s", tag)
rm.changes <- ""
})
rm.collectsCh = make(chan []string)
go collectedHandler(rm)
go changesHandler(rm)
go fsnotifyHandler(rm)
}
func (rm *RecursiveMonitor) SubscribeToChanges(subscriber chan<- string) {
rm.changesObservers = append(rm.changesObservers, subscriber)
log.Debugf("Added changes observer: %d.", len(rm.changesObservers))
}
func (rm *RecursiveMonitor) Subscribe(subscriber chan<- []string) {
rm.collectObservers = append(rm.collectObservers, subscriber)
log.Debugf("Added collect observer: %d.", len(rm.collectObservers))
}
func (rm *RecursiveMonitor) Stop() {
rm.watcher.Close()
}
// add a directory to be monitored
func (rm *RecursiveMonitor) add(path string) {
path = filepath.Clean(path)
h := fnv.New32a()
h.Write([]byte(path))
hash := h.Sum32()
m := MonitorPath{
path: path,
}
rm.monitors[hash] = m
err := rm.watcher.Add(path)
if err != nil {
log.Fatalf("Error adding path to RecursiveMonitor %s: %s", path, err)
}
}
// Remove a potentially watched directory from the monitor.
// Return true if the directory was watched and removed.
func (rm *RecursiveMonitor) remove(path string) bool {
path = filepath.Clean(path)
h := fnv.New32a()
h.Write([]byte(path))
hash := h.Sum32()
if _, ok := rm.monitors[hash]; ok {
// if the path was monitored, remove it.
err := rm.watcher.Remove(path)
if err != nil {
log.Debugf("Error removing path %s: %s", path, err)
} else {
delete(rm.monitors, hash)
return true
}
} // do nothing if the path wasn't monitored.
return false
}
// Number of watched directories
func (rm *RecursiveMonitor) length() int {
return len(rm.monitors)
}
// Return the watched directories as a string array, in random order.
func (rm *RecursiveMonitor) watchedDirectories() []string {
dir := make([]string, 0)
for _, m := range rm.monitors {
dir = append(dir, m.path)
}
return dir
}
// notify messages to all change observers
func (rm *RecursiveMonitor) notifyChanges(msg string) {
// log.Debugf("notify %d changes observers: %s", len(rm.changesObservers), msg)
for _, sub := range rm.changesObservers {
sub <- msg
}
}
func (rm *RecursiveMonitor) notifyCollected(collected []string) {
log.Debugf("notify %d collect observers: %d messages.", len(rm.collectObservers), len(collected))
for _, obs := range rm.collectObservers {
obs <- collected
}
}
// Listen to fsnotify events and post to the changes channel.
func fsnotifyHandler(rmPtr *RecursiveMonitor) {
for {
select {
case event, ok := <-rmPtr.watcher.Events:
if !ok {
log.Debugf("Watcher event is not ok.")
return
}
filename := filepath.Clean(event.Name)
switch event.Op {
case fsnotify.Create:
info, err := os.Stat(filename)
if err != nil {
log.Infof("File error %s: %s", filename, err)
} else if info.IsDir() { // a new directory was added
rmPtr.add(filename)
}
rmPtr.changes <- filename
case fsnotify.Remove:
if rmPtr.remove(filename) {
// this is actually a dead block, I don't believe we'll go into this in any situation.
dir := filepath.Dir(filename)
rmPtr.changes <- dir
} else {
rmPtr.changes <- filename
}
default:
rmPtr.changes <- filename
}
log.Debugf("fsnotify event: %s", event)
case err, ok := <-rmPtr.watcher.Errors:
if !ok {
log.Errorf("Watcher errors is not ok.")
return
}
log.Errorf("error: %s", err)
}
}
}
func changesHandler(rmPtr *RecursiveMonitor) {
for {
msg := <-rmPtr.changes
switch msg {
case "": // collect event
// log.Debugf("Collecting monitored changes: %s", rmPtr.accumulated)
if rmPtr.accumulated == nil {
// this is very wrong, accumulated should have at least one msg.
log.Fatalln("Try to collect an empty accumulated list.")
} else {
rmPtr.collectsCh <- rmPtr.accumulated
rmPtr.accumulated = nil
}
default:
if rmPtr.accumulated == nil {
rmPtr.accumulated = make([]string, 0)
}
if rmPtr.basedir != "" && !strings.HasPrefix(msg, rmPtr.basedir) {
// make sure the path is full
msg = path.Join(rmPtr.basedir, msg)
}
// rmPtr.accumulated = append(rmPtr.accumulated, msg)
rmPtr.accumulated = *rmPtr.accumulated.Add(msg)
rmPtr.debouncer.Event(msg)
rmPtr.notifyChanges(msg)
}
}
}
// Relay the collected message to observers.
func collectedHandler(rmPtr *RecursiveMonitor) {
for {
collected := <-rmPtr.collectsCh
rmPtr.notifyCollected(collected)
}
}
// Traverse the tree and create monitor for all directory
func NewRecursiveMonitor(start string) RecursiveMonitor {
monitors := make(map[uint32]MonitorPath)
watcherPtr, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Error creating watcherPtr: %s", err)
}
rm := RecursiveMonitor{
basedir: start,
monitors: monitors,
watcher: watcherPtr,
changes: make(chan string, 10),
changesObservers: make([]chan<- string, 0),
}
log.Debugf("Walking: %s", start)
err = filepath.Walk(start, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
log.Debugf(" | Added: %s", path)
rm.add(path)
}
return nil
})
if err != nil {
log.Fatalf("Error walking the directory tree %s: %s.", start, err)
}
return rm
}