forked from birkirb/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
299 lines (263 loc) · 8.46 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/fsnotify/fsnotify"
"time"
)
// Listener is an interface for receivers of filesystem events.
type Listener interface {
// Refresh is invoked by the watcher on relevant filesystem events.
// If the listener returns an error, it is served to the user on the current request.
Refresh() *Error
}
// DiscerningListener allows the receiver to selectively watch files.
type DiscerningListener interface {
Listener
WatchDir(info os.FileInfo) bool
WatchFile(basename string) bool
}
// Watcher allows listeners to register to be notified of changes under a given
// directory.
type Watcher struct {
serial bool // true to process events in serial
watchers []*fsnotify.Watcher // Parallel arrays of watcher/listener pairs.
listeners []Listener // List of listeners for watcher
forceRefresh bool // True to force the refresh
lastError int // The last error found
notifyMutex sync.Mutex // The mutext to serialize watches
refreshTimer *time.Timer // The timer to countdown the next refresh
timerMutex *sync.Mutex // A mutex to prevent concurrent updates
refreshChannel chan *Error // The error channel to listen to when waiting for a refresh
refreshChannelCount int // The number of clients listening on the channel
refreshTimerMS time.Duration // The number of milliseconds between refreshing builds
}
func NewWatcher() *Watcher {
return &Watcher{
forceRefresh: true,
lastError: -1,
refreshTimerMS: time.Duration(Config.IntDefault("watch.rebuild.delay", 10)),
timerMutex: &sync.Mutex{},
refreshChannel: make(chan *Error, 10),
refreshChannelCount: 0,
}
}
// Listen registers for events within the given root directories (recursively).
func (w *Watcher) Listen(listener Listener, roots ...string) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
utilLog.Fatal("Watcher: Failed to create watcher", "error", err)
}
// Replace the unbuffered Event channel with a buffered one.
// Otherwise multiple change events only come out one at a time, across
// multiple page views. (There appears no way to "pump" the events out of
// the watcher)
// This causes a notification when you do a check in go, since you are modifying a buffer in use
watcher.Events = make(chan fsnotify.Event, 100)
watcher.Errors = make(chan error, 10)
// Walk through all files / directories under the root, adding each to watcher.
for _, p := range roots {
// is the directory / file a symlink?
f, err := os.Lstat(p)
if err == nil && f.Mode()&os.ModeSymlink == os.ModeSymlink {
var realPath string
realPath, err = filepath.EvalSymlinks(p)
if err != nil {
panic(err)
}
p = realPath
}
fi, err := os.Stat(p)
if err != nil {
utilLog.Error("Watcher: Failed to stat watched path, code will continue but auto updates will not work", "path", p, "error", err)
continue
}
// If it is a file, watch that specific file.
if !fi.IsDir() {
err = watcher.Add(p)
if err != nil {
utilLog.Error("Watcher: Failed to watch, code will continue but auto updates will not work", "path", p, "error", err)
}
continue
}
var watcherWalker func(path string, info os.FileInfo, err error) error
watcherWalker = func(path string, info os.FileInfo, err error) error {
if err != nil {
utilLog.Error("Watcher: Error walking path:", "error", err)
return nil
}
if info.IsDir() {
if dl, ok := listener.(DiscerningListener); ok {
if !dl.WatchDir(info) {
return filepath.SkipDir
}
}
err := watcher.Add(path)
if err != nil {
utilLog.Error("Watcher: Failed to watch this path, code will continue but auto updates will not work", "path", path, "error", err)
}
}
return nil
}
// Else, walk the directory tree.
err = Walk(p, watcherWalker)
if err != nil {
utilLog.Error("Watcher: Failed to walk directory, code will continue but auto updates will not work", "path", p, "error", err)
}
}
if w.eagerRebuildEnabled() {
// Create goroutine to notify file changes in real time
go w.NotifyWhenUpdated(listener, watcher)
}
w.watchers = append(w.watchers, watcher)
w.listeners = append(w.listeners, listener)
}
// NotifyWhenUpdated notifies the watcher when a file event is received.
func (w *Watcher) NotifyWhenUpdated(listener Listener, watcher *fsnotify.Watcher) {
for {
select {
case ev := <-watcher.Events:
if w.rebuildRequired(ev, listener) {
// Serialize listener.Refresh() calls.
if w.serial {
// Serialize listener.Refresh() calls.
w.notifyMutex.Lock()
if err := listener.Refresh(); err != nil {
utilLog.Error("Watcher: Listener refresh reported error:", "error", err)
}
w.notifyMutex.Unlock()
} else {
// Run refresh in parallel
go func() {
w.notifyInProcess(listener)
}()
}
}
case <-watcher.Errors:
continue
}
}
}
// Notify causes the watcher to forward any change events to listeners.
// It returns the first (if any) error returned.
func (w *Watcher) Notify() *Error {
// Serialize Notify() calls.
w.notifyMutex.Lock()
defer w.notifyMutex.Unlock()
for i, watcher := range w.watchers {
listener := w.listeners[i]
// Pull all pending events / errors from the watcher.
refresh := false
for {
select {
case ev := <-watcher.Events:
if w.rebuildRequired(ev, listener) {
refresh = true
}
continue
case <-watcher.Errors:
continue
default:
// No events left to pull
}
break
}
if w.forceRefresh || refresh || w.lastError == i {
var err *Error
if w.serial {
err = listener.Refresh()
} else {
err = w.notifyInProcess(listener)
}
if err != nil {
w.lastError = i
return err
}
}
}
w.forceRefresh = false
w.lastError = -1
return nil
}
// Build a queue for refresh notifications
// this will not return until one of the queue completes
func (w *Watcher) notifyInProcess(listener Listener) (err *Error) {
shouldReturn := false
// This code block ensures that either a timer is created
// or that a process would be added the the h.refreshChannel
func() {
w.timerMutex.Lock()
defer w.timerMutex.Unlock()
// If we are in the process of a rebuild, forceRefresh will always be true
w.forceRefresh = true
if w.refreshTimer != nil {
utilLog.Info("Found existing timer running, resetting")
w.refreshTimer.Reset(time.Millisecond * w.refreshTimerMS)
shouldReturn = true
w.refreshChannelCount++
} else {
w.refreshTimer = time.NewTimer(time.Millisecond * w.refreshTimerMS)
}
}()
// If another process is already waiting for the timer this one
// only needs to return the output from the channel
if shouldReturn {
return <-w.refreshChannel
}
utilLog.Info("Waiting for refresh timer to expire")
<-w.refreshTimer.C
w.timerMutex.Lock()
// Ensure the queue is properly dispatched even if a panic occurs
defer func() {
for x := 0; x < w.refreshChannelCount; x++ {
w.refreshChannel <- err
}
w.refreshChannelCount = 0
w.refreshTimer = nil
w.timerMutex.Unlock()
}()
err = listener.Refresh()
if err != nil {
utilLog.Info("Watcher: Recording error last build, setting rebuild on", "error", err)
} else {
w.lastError = -1
w.forceRefresh = false
}
//utilLog.Info("Rebuilt, result", "error", err)
return
}
// If watch.mode is set to eager, the application is rebuilt immediately
// when a source file is changed.
// This feature is available only in dev mode.
func (w *Watcher) eagerRebuildEnabled() bool {
return Config.BoolDefault("mode.dev", true) &&
Config.BoolDefault("watch", true) &&
Config.StringDefault("watch.mode", "normal") == "eager"
}
func (w *Watcher) rebuildRequired(ev fsnotify.Event, listener Listener) bool {
// Ignore changes to dotfiles.
if strings.HasPrefix(filepath.Base(ev.Name), ".") {
return false
}
if dl, ok := listener.(DiscerningListener); ok {
if !dl.WatchFile(ev.Name) || ev.Op&fsnotify.Chmod == fsnotify.Chmod {
return false
}
}
return true
}
var WatchFilter = func(c *Controller, fc []Filter) {
if MainWatcher != nil {
err := MainWatcher.Notify()
if err != nil {
c.Result = c.RenderError(err)
return
}
}
fc[0](c, fc[1:])
}