/
log_watcher.go
103 lines (90 loc) · 2.37 KB
/
log_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
// Copyright 2015 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
package watcher
import (
"expvar"
"fmt"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/golang/glog"
)
var (
eventCount = expvar.NewMap("log_watcher_event_count")
errorCount = expvar.NewInt("log_watcher_error_count")
)
// LogWatcher implements a Watcher for watching real filesystems.
type LogWatcher struct {
*fsnotify.Watcher
events []chan Event
eventsMu sync.RWMutex
runDone chan struct{} // Channel to respond to Close
}
// NewLogWatcher returns a new LogWatcher, or returns an error.
func NewLogWatcher() (*LogWatcher, error) {
f, err := fsnotify.NewWatcher()
if err == nil {
w := &LogWatcher{
Watcher: f,
events: make([]chan Event, 0),
runDone: make(chan struct{}),
}
go w.run()
return w, nil
}
return nil, err
}
// Events returns a new readable channel of events from this watcher.
func (w *LogWatcher) Events() <-chan Event {
w.eventsMu.Lock()
defer w.eventsMu.Unlock()
r := make(chan Event, 1)
w.events = append(w.events, r)
return r
}
func (w *LogWatcher) sendEvent(e Event) {
w.eventsMu.RLock()
for _, c := range w.events {
c <- e
}
w.eventsMu.RUnlock()
}
func (w *LogWatcher) run() {
defer close(w.runDone)
// Suck out errors and dump them to the error log.
go func() {
for err := range w.Watcher.Errors {
errorCount.Add(1)
glog.Errorf("fsnotify error: %s\n", err)
}
}()
for e := range w.Watcher.Events {
glog.V(2).Infof("watcher event %v", e)
eventCount.Add(e.Name, 1)
switch {
case e.Op&fsnotify.Create == fsnotify.Create:
w.sendEvent(CreateEvent{e.Name})
case e.Op&fsnotify.Write == fsnotify.Write,
e.Op&fsnotify.Chmod == fsnotify.Chmod:
w.sendEvent(UpdateEvent{e.Name})
case e.Op&fsnotify.Remove == fsnotify.Remove:
w.sendEvent(DeleteEvent{e.Name})
case e.Op&fsnotify.Rename == fsnotify.Rename:
// Rename is only issued on the original file path; the new name receives a Create event
w.sendEvent(DeleteEvent{e.Name})
default:
panic(fmt.Sprintf("unknown op type %v", e.Op))
}
}
glog.Infof("Shutting down log watcher.")
w.eventsMu.Lock()
for _, c := range w.events {
close(c)
}
w.eventsMu.Unlock()
}
// Close shuts down the LogWatcher. It is safe to call this from multiple clients because
func (w *LogWatcher) Close() (err error) {
err = w.Watcher.Close()
<-w.runDone
return
}