-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
198 lines (167 loc) · 4.03 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
package watcher
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/apoxy-dev/proximal/core/log"
middlewarev1 "github.com/apoxy-dev/proximal/api/middleware/v1"
)
// Watcher watches a directory for changes and triggers a build when a change is
// detected.
type Watcher struct {
// IgnoreRegex is a regex of files to ignore.
IgnoreRegex string
client middlewarev1.MiddlewareServiceClient
wr *fsnotify.Watcher
mu sync.Mutex
// watches is a map of slugs to watch directories.
watches map[string]string
// subpaths is a map of subpaths to slugs.
subpaths map[string]string
// batches is a map of slugs to whether a build has been triggered.
batches map[string]bool
}
// NewWatcher creates a new Watcher.
func NewWatcher(c middlewarev1.MiddlewareServiceClient, ignoreRegex string) *Watcher {
return &Watcher{
IgnoreRegex: ignoreRegex,
client: c,
watches: make(map[string]string),
subpaths: make(map[string]string),
batches: make(map[string]bool),
}
}
// Run starts the watcher until the context is cancelled.
func (w *Watcher) Run(ctx context.Context) error {
var err error
w.wr, err = fsnotify.NewWatcher()
if err != nil {
return err
}
defer w.wr.Close()
r, err := regexp.Compile(w.IgnoreRegex)
if err != nil {
return fmt.Errorf("failed to compile ignore regex: %w", err)
}
errCh := make(chan error, 1)
go func() {
var err error
defer func() {
errCh <- err
}()
for {
select {
case e, ok := <-w.wr.Events:
if !ok {
err = errors.New("watcher closed")
return
}
log.Infof("change detected in %s: %s", e.Name, e.Op)
if e.Op&(fsnotify.Create|fsnotify.Write|fsnotify.Remove|fsnotify.Rename) != 0 {
if r.MatchString(filepath.Base(e.Name)) {
log.Debugf("ignoring %s", e.Name)
continue
}
slug, ok := w.subpaths[filepath.Dir(e.Name)]
if !ok {
log.Errorf("no slug found for %s", e.Name)
continue
}
log.Infof("batching re-build for slug %s due to %s", slug, e.Name)
w.mu.Lock()
w.batches[slug] = true
w.mu.Unlock()
}
case err, ok := <-w.wr.Errors:
if !ok {
err = errors.New("watcher closed")
return
}
log.Errorf("watcher error: %v", err)
case <-ctx.Done():
err = ctx.Err()
return
}
}
}()
go func() {
for {
select {
case <-time.After(2 * time.Second):
w.mu.Lock()
for slug := range w.batches {
log.Infof("triggering build for %s", slug)
if _, err := w.client.TriggerBuild(ctx, &middlewarev1.TriggerBuildRequest{
Slug: slug,
}); err != nil {
log.Errorf("failed to trigger build: %v", err)
}
}
w.batches = make(map[string]bool)
w.mu.Unlock()
case <-ctx.Done():
return
}
}
}()
return <-errCh
}
// Add adds a new watch.
func (w *Watcher) Add(slug, dir string) error {
w.mu.Lock()
defer w.mu.Unlock()
dir, err := filepath.Abs(dir)
if err != nil {
return err
}
if d, ok := w.watches[slug]; ok && d != dir {
return fmt.Errorf("slug %s already exists with dir %s", slug, d)
}
w.watches[slug] = dir
log.Infof("watching %s", dir)
// Walk dir tree and place watchs for each dir (watches are not recursive).
if err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() || info.Name() == ".git" {
return nil
}
log.Debugf("watching %s", path)
if err := w.wr.Add(path); err != nil {
return err
}
w.subpaths[path] = slug
return nil
}); err != nil {
return err
}
return nil
}
// Remove removes a watch.
func (w *Watcher) Remove(slug string) error {
w.mu.Lock()
defer w.mu.Unlock()
dir, ok := w.watches[slug]
if !ok {
return fmt.Errorf("slug %s does not exist", slug)
}
// Removes all paths beginning with dir.
for _, path := range w.wr.WatchList() {
if !filepath.HasPrefix(path, dir) {
continue
}
if err := w.wr.Remove(path); err != nil {
return err
}
delete(w.subpaths, path)
}
delete(w.watches, slug)
return nil
}