-
Notifications
You must be signed in to change notification settings - Fork 43
/
watcher.go
191 lines (160 loc) · 3.88 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
package watcher
import (
"context"
"errors"
"fmt"
s "strings"
"sync"
fsutils "github.com/FleekHQ/space-daemon/core/space/services"
"github.com/mitchellh/go-homedir"
"time"
"github.com/radovskyb/watcher"
"github.com/FleekHQ/space-daemon/log"
)
var (
ErrFolderPathNotFound = errors.New("could not find a folder path for watcher")
)
type FolderWatcher interface {
RegisterHandler(handler EventHandler)
AddFile(path string) error
Watch(ctx context.Context) error
Close()
}
type folderWatcher struct {
w *watcher.Watcher
lock sync.Mutex
publishLock sync.RWMutex
options watcherOptions
started bool
closed bool
handlers []EventHandler
}
// New creates an new instance of folder watcher
func New(configs ...Option) (*folderWatcher, error) {
options := watcherOptions{}
for _, config := range configs {
config(&options)
}
w := watcher.New()
for _, path := range options.paths {
if home, err := homedir.Dir(); err == nil {
// If the root directory contains ~, we replace it with the actual home directory
path = s.Replace(path, "~", home, -1)
}
if path == "" {
log.Fatal(ErrFolderPathNotFound)
return nil, ErrFolderPathNotFound
}
err := w.AddRecursive(path)
if err != nil {
return nil, err
}
}
return &folderWatcher{
w: w,
options: options,
}, nil
}
func (fw *folderWatcher) RegisterHandler(handler EventHandler) {
fw.publishLock.Lock()
defer fw.publishLock.Unlock()
fw.handlers = append(fw.handlers, handler)
}
func (fw *folderWatcher) AddFile(path string) error {
if fsutils.IsPathDir(path) {
return errors.New(fmt.Sprintf("unable to watch path %s folder is not supporter", path))
}
err := fw.w.Add(path)
if err != nil {
return err
}
return err
}
// Watch will start listening of changes on the folderWatcher path and trigger the handler with any update events
// This is a block operation
func (fw *folderWatcher) Watch(ctx context.Context) error {
fw.setToStarted()
go func() {
for {
select {
case <-fw.w.Closed:
log.Debug("Watcher graceful shutdown triggered")
return
case <-ctx.Done():
fw.Close()
case event, ok := <-fw.w.Event:
if ok {
if len(fw.handlers) == 0 {
fw.publishEventToHandler(ctx, &defaultWatcherHandler{}, event)
} else {
fw.publishEvent(ctx, event)
}
}
case err, ok := <-fw.w.Error:
if !ok {
return
}
log.Fatal(err)
}
}
}()
log.Debug("Starting watcher", fmt.Sprintf("filePath:%s", fw.options.paths))
// This is blocking
err := fw.w.Start(time.Millisecond * 100)
fw.started = false
if err != nil {
return err
}
return nil
}
func (fw *folderWatcher) setToStarted() {
fw.lock.Lock()
defer fw.lock.Unlock()
if fw.started {
return
}
fw.started = true
}
func (fw *folderWatcher) publishEvent(ctx context.Context, event watcher.Event) {
fw.publishLock.RLock()
defer fw.publishLock.RUnlock()
for _, handler := range fw.handlers {
fw.publishEventToHandler(ctx, handler, event)
}
}
func (fw *folderWatcher) publishEventToHandler(
ctx context.Context,
handler EventHandler,
event watcher.Event,
) {
if isBlacklisted(event.Path, event.FileInfo) {
log.Debug("Skipping blacklisted file/folder event")
return
}
switch event.Op {
case watcher.Create:
handler.OnCreate(ctx, event.Path, event.FileInfo)
case watcher.Remove:
handler.OnRemove(ctx, event.Path, event.FileInfo)
case watcher.Write:
handler.OnWrite(ctx, event.Path, event.FileInfo)
case watcher.Rename:
handler.OnRename(ctx, event.Path, event.FileInfo, event.OldPath)
case watcher.Move:
handler.OnMove(ctx, event.Path, event.FileInfo, event.OldPath)
}
}
// Close will stop the watching operation and unblock watch calls
func (fw *folderWatcher) Close() {
fw.lock.Lock()
defer fw.lock.Unlock()
if !fw.started || fw.closed {
return
}
fw.closed = true
fw.w.Close()
}
func (fw *folderWatcher) Shutdown() error {
fw.Close()
return nil
}