/
watcher.go
231 lines (181 loc) · 4.84 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
package watcher
import (
"context"
"errors"
"fmt"
"io"
"os"
"sync"
"time"
"gocloud.dev/blob"
)
var (
ErrWatchTimeout = errors.New("watcher timed out")
ErrBucketMismatch = errors.New("bucket mismatch")
)
type Watcher interface {
// Watch waits until a blob is dispatched.
Watch(ctx context.Context) (*BlobEvent, error)
// OpenBucket returns the bucket where the blobs can be found.
OpenBucket(ctx context.Context) (*blob.Bucket, error)
// Pipelines retunrs the names of the subset of pipelines to be used.
Pipelines() []string
RetentionPeriod() *time.Duration
CompletedDir() string
StripTopLevelDir() bool
RejectDuplicates() bool
ExcludeHiddenFiles() bool
TransferType() string
// Full path of the watched bucket when available, empty string otherwise.
Path() string
fmt.Stringer // It should return the name of the watcher.
}
type commonWatcherImpl struct {
name string
pipeline []string
retentionPeriod *time.Duration
completedDir string
stripTopLevelDir bool
rejectDuplicates bool
excludeHiddenFiles bool
transferType string
}
func (w *commonWatcherImpl) String() string {
return w.name
}
func (w *commonWatcherImpl) Pipelines() []string {
return w.pipeline
}
func (w *commonWatcherImpl) RetentionPeriod() *time.Duration {
return w.retentionPeriod
}
func (w *commonWatcherImpl) CompletedDir() string {
return w.completedDir
}
func (w *commonWatcherImpl) StripTopLevelDir() bool {
return w.stripTopLevelDir
}
func (w *commonWatcherImpl) RejectDuplicates() bool {
return w.rejectDuplicates
}
func (w *commonWatcherImpl) ExcludeHiddenFiles() bool {
return w.excludeHiddenFiles
}
func (w *commonWatcherImpl) TransferType() string {
return w.transferType
}
type Service interface {
// Watchers return all known watchers.
Watchers() []Watcher
// Return a watcher given its name.
ByName(name string) (Watcher, error)
// Download blob given an event.
Download(ctx context.Context, w io.Writer, watcherName, key string) error
// Delete blob given an event.
Delete(ctx context.Context, watcherName, key string) error
// Dipose blob into the completedDir directory.
Dispose(ctx context.Context, watcherName, key string) error
}
type serviceImpl struct {
watchers map[string]Watcher
mu sync.RWMutex
}
var _ Service = (*serviceImpl)(nil)
func New(ctx context.Context, c *Config) (*serviceImpl, error) {
watchers := map[string]Watcher{}
for _, item := range c.Minio {
item := item
w, err := NewMinioWatcher(ctx, item)
if err != nil {
return nil, err
}
watchers[item.Name] = w
}
for _, item := range c.Filesystem {
item := item
w, err := NewFilesystemWatcher(ctx, item)
if err != nil {
return nil, err
}
watchers[item.Name] = w
}
if len(watchers) == 0 {
return nil, errors.New("there are not watchers configured")
}
return &serviceImpl{watchers: watchers}, nil
}
func (svc *serviceImpl) Watchers() []Watcher {
svc.mu.RLock()
defer svc.mu.RUnlock()
ww := []Watcher{}
for _, item := range svc.watchers {
item := item
ww = append(ww, item)
}
return ww
}
func (svc *serviceImpl) watcher(name string) (Watcher, error) {
svc.mu.RLock()
defer svc.mu.RUnlock()
w, ok := svc.watchers[name]
if !ok {
return nil, fmt.Errorf("error loading watcher: unknown watcher %s", name)
}
return w, nil
}
func (svc *serviceImpl) ByName(name string) (Watcher, error) {
return svc.watcher(name)
}
func (svc *serviceImpl) Download(ctx context.Context, writer io.Writer, watcherName, key string) error {
w, err := svc.watcher(watcherName)
if err != nil {
return err
}
bucket, err := w.OpenBucket(ctx)
if err != nil {
return fmt.Errorf("error opening bucket: %w", err)
}
defer bucket.Close()
reader, err := bucket.NewReader(ctx, key, nil)
if err != nil {
return fmt.Errorf("error creating reader: %w", err)
}
defer reader.Close()
if _, err := io.Copy(writer, reader); err != nil {
return fmt.Errorf("error copying blob: %w", err)
}
return nil
}
func (svc *serviceImpl) Delete(ctx context.Context, watcherName, key string) error {
w, err := svc.watcher(watcherName)
if err != nil {
return err
}
bucket, err := w.OpenBucket(ctx)
if err != nil {
return fmt.Errorf("error opening bucket: %w", err)
}
defer bucket.Close()
// Exceptionally, a filesystem-based watcher may be dealing with a
// directory instead of a regular fileblob.
var fi os.FileInfo
if bucket.As(&fi) && fi.IsDir() {
fw, ok := w.(*filesystemWatcher)
if !ok {
return fmt.Errorf("error removing directory: %s", err)
}
return fw.RemoveAll(key)
}
return bucket.Delete(ctx, key)
}
func (svc *serviceImpl) Dispose(ctx context.Context, watcherName, key string) error {
w, err := svc.watcher(watcherName)
if err != nil {
return err
}
fw, ok := w.(*filesystemWatcher)
if !ok {
return fmt.Errorf("not available in this type of watcher: %s", err)
}
return fw.Dispose(key)
}