-
Notifications
You must be signed in to change notification settings - Fork 2
/
watcher.go
272 lines (226 loc) · 6.14 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
package watcher
import (
"fmt"
"io"
"log"
"os"
"path"
"regexp"
"strings"
"sync"
"time"
"github.com/Leryan/watchngo/pkg/utils"
"github.com/fsnotify/fsnotify"
)
// Watcher ...
type Watcher struct {
Name string
Command string
Match string
Filter string
FSWatcher *fsnotify.Watcher
Shell string
Debug bool
Logger *log.Logger
executor Executor
eLock sync.RWMutex
filter *regexp.Regexp
eventQueue chan fsnotify.Event
}
// Find add files to the watcher. Currently only one file with it's exact
// path (may be relative) is supported.
func (w *Watcher) Find() error {
matchstat, err := os.Stat(w.Match)
wr := utils.NewWalkRec()
if err == nil && matchstat.IsDir() {
wr, err = utils.FindRecursive(w.Match, wr)
if err != nil {
return fmt.Errorf("find: %v", err)
}
if w.Debug {
w.Logger.Printf("watcher %s found recursive directories", w.Name)
}
} else if err == nil && !matchstat.IsDir() {
wr.Matches = append(wr.Matches, w.Match)
if w.Debug {
w.Logger.Printf("watcher %s use single file", w.Name)
}
} else if err != nil {
wr.Matches, err = utils.FindGlob(w.Match, wr.Matches)
if err != nil {
return fmt.Errorf("glob: %v", err)
} else if len(wr.Matches) == 0 {
return fmt.Errorf("empty glob: %s", w.Match)
}
if w.Debug {
w.Logger.Printf("watcher %s use glob match", w.Name)
}
} else {
return fmt.Errorf("bad conf: %s: %v", w.Match, err)
}
if w.Filter != "" {
rfilter, err := regexp.Compile(w.Filter)
if err != nil {
return fmt.Errorf("filter: %s: %v", w.Filter, rfilter)
}
w.filter = rfilter
}
for _, match := range wr.Matches {
if w.Debug {
w.Logger.Printf("add match: %s", match)
}
err := w.FSWatcher.Add(match)
if err != nil {
return fmt.Errorf("on match: %s: %v", match, err)
}
}
return nil
}
func (w *Watcher) exec(command string, output io.Writer) {
w.Logger.Printf("running command on watcher %s", w.Name)
err := w.executor.Exec(command)
if err == nil {
w.Logger.Printf("finished running command on watcher %s", w.Name)
} else {
w.Logger.Printf("finished running command on watcher %s with error: %v", w.Name, err)
}
}
func (w *Watcher) handleFSEvent(event fsnotify.Event, executed bool) bool {
eventFile := path.Clean(event.Name)
command := strings.Replace(w.Command, "%match", w.Match, -1)
command = strings.Replace(command, "%filter", w.Filter, -1)
command = strings.Replace(command, "%event.file", eventFile, -1)
command = strings.Replace(command, "%event.op", event.Op.String(), -1)
if w.Debug {
w.Logger.Printf("event: %v", event)
w.Logger.Printf("command: %v", command)
}
if w.filter != nil && !w.filter.MatchString(eventFile) {
return false
}
if eventFile == "" {
return false
}
isWrite := fsnotify.Write&event.Op == fsnotify.Write
isRemove := fsnotify.Remove&event.Op == fsnotify.Remove
isChmod := fsnotify.Chmod&event.Op == fsnotify.Chmod
//isCreate := fsnotify.Create&event.Op == fsnotify.Create
isRename := fsnotify.Rename&event.Op == fsnotify.Rename
eventFileStat, err := os.Stat(eventFile)
if err != nil && !isRemove && !isRename {
w.Logger.Printf("worker: %s: %v", eventFile, err)
return false
}
isFile := false
isDir := false
if eventFileStat != nil {
isFile = !eventFileStat.IsDir()
isDir = eventFileStat.IsDir()
}
if w.executor.Running() {
if w.Debug {
w.Logger.Printf("already running, ignoring")
}
return false
}
mustExec := false
if (isWrite || isChmod) && isFile {
mustExec = true
} else if isRemove || isRename {
w.FSWatcher.Remove(eventFile)
retries := 0
for retries < 10 {
_, err := os.Stat(eventFile)
if err == nil {
w.FSWatcher.Add(eventFile)
mustExec = true
break
} else if w.Debug {
w.Logger.Printf("re-add attempt: %v", err)
}
time.Sleep(time.Millisecond * 100)
retries++
}
if retries == 10 {
w.Logger.Printf("cannot re-add file: %s", eventFile)
}
} else if isDir {
mustExec = true
}
if mustExec && !executed {
w.exec(command, os.Stdout)
}
return mustExec
}
func (w *Watcher) eventQueueConsumer() {
timerInterval := time.Millisecond * 500
timer := time.NewTimer(timerInterval)
evtDate := time.Now()
events := make([]fsnotify.Event, 0)
for {
select {
case event := <-w.eventQueue:
events = append(events, event)
evtDate = time.Now()
case <-timer.C:
if time.Now().Sub(evtDate) > timerInterval && len(events) > 0 {
if w.Debug {
w.Logger.Printf("sending %d events", len(events))
w.Logger.Printf("events: %v", events)
}
executed := false
for _, event := range events {
executed = executed || w.handleFSEvent(event, executed)
}
events = make([]fsnotify.Event, 0)
evtDate = time.Now()
}
timer.Reset(timerInterval)
}
}
}
// Work fires the watcher and run commands when an event is received.
func (w *Watcher) Work() error {
w.eventQueue = make(chan fsnotify.Event)
go w.eventQueueConsumer()
w.Logger.Printf("running watcher %v", w.Name)
for {
select {
case event := <-w.FSWatcher.Events:
w.eventQueue <- event
case err := <-w.FSWatcher.Errors:
w.Logger.Printf("error: %v, watcher %s stopped", err, w.Name)
w.FSWatcher.Close()
return err
}
}
}
// NewWatcher requires logger and executor to not be nil.
// param name is purely cosmetic, for logs.
// param match is a file, directory or "glob" path (shell-like).
// param command is a single command to run through executor.
// param executor is an instance of Executor that is not required to honor the given command, like for the Print executor.
// param debug shows more details when running.
// param logger must log to stderr when using executor Print.
func NewWatcher(name, match, filter, command string, executor Executor, debug bool, logger *log.Logger) (*Watcher, error) {
fswatcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
if logger == nil {
return nil, fmt.Errorf("logger cannot be nil")
}
if executor == nil {
return nil, fmt.Errorf("executor cannot be nil")
}
return &Watcher{
Name: name,
Filter: filter,
Command: command,
FSWatcher: fswatcher,
Match: match,
Logger: logger,
Debug: debug,
executor: executor,
}, nil
}