-
Notifications
You must be signed in to change notification settings - Fork 1
/
polywatch.go
275 lines (215 loc) · 5.2 KB
/
polywatch.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
package polywatch
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/radovskyb/watcher"
"github.com/zmwangx/debounce"
"github.com/pouyanh/polywatch/config"
)
var (
ErrUnsupportedFilter = errors.New("filter not supported")
)
func Start() error {
ctx, stop := context.WithCancel(context.Background())
defer stop()
cfg := config.MustLoad()
wg := sync.WaitGroup{}
for _, cw := range cfg.Watchers {
w, err := newPolyWatcher(cw)
if err != nil {
return err
}
wg.Add(1)
go func() {
defer wg.Done()
if err := w.watch(ctx); err != nil {
stop()
}
}()
}
bindSignals(stop, syscall.SIGTERM, syscall.SIGINT)
wg.Wait()
return nil
}
func bindSignals(fn func(), ss ...os.Signal) {
ntfy := make(chan os.Signal, 1)
signal.Notify(ntfy, ss...)
go func() {
<-ntfy
fn()
}()
}
type polyWatcher struct {
cfg config.Watcher
w *watcher.Watcher
lg *log.Logger
cmd *exec.Cmd
}
func newPolyWatcher(cfg config.Watcher) (*polyWatcher, error) {
lg := log.New(os.Stderr, fmt.Sprintf("poly-watcher[%s]: ", cfg.Name), log.LstdFlags)
w := watcher.New()
for _, wf := range cfg.Watch.Files {
path := filepath.Clean(os.ExpandEnv(wf.Path))
var err error
if wf.Recursive {
err = w.AddRecursive(path)
} else {
err = w.Add(path)
}
if err != nil {
return nil, err
}
}
for _, wf := range cfg.Watch.Filters {
switch wf.On {
case config.WatchFilterScopeFilename:
switch wf.Type {
case config.WatchFilterTypeRegex:
w.AddFilterHook(fileFilterRegex(wf.Include, wf.List...))
case config.WatchFilterTypeList:
w.AddFilterHook(fileFilterList(wf.Include, wf.List...))
default:
return nil, ErrUnsupportedFilter
}
default:
// todo: event filters
lg.Printf(`filter scope "%s" not supported yet`, wf.On)
}
}
pw := &polyWatcher{
cfg: cfg,
w: w,
lg: lg,
}
pw.renewCommand()
return pw, nil
}
func (pw *polyWatcher) renewCommand() {
// todo: support multiline command
rawcmd := strings.Split(pw.cfg.Command.Shell, " ")
rawcmd = append(rawcmd, pw.cfg.Command.Exec)
cmd := exec.Command(rawcmd[0], rawcmd[1:]...)
cmd.Env = pw.cfg.Command.Env
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pw.cmd = cmd
}
func (pw *polyWatcher) watch(ctx context.Context) error {
for path, f := range pw.w.WatchedFiles() {
pw.lg.Printf("%s: %s\n", path, f.Name())
}
chErr := make(chan error)
defer close(chErr)
uh := pw.updateHandler()
go func() {
for {
select {
case e := <-pw.w.Event:
pw.lg.Printf("event received: %+v\n", e)
err := uh(ctx, e)
if err != nil {
pw.lg.Printf("error occurred during handling update: %s\n", err)
}
case err := <-pw.w.Error:
chErr <- err
case <-pw.w.Closed:
return
}
}
}()
go func() {
go func() {
pw.w.Wait()
pw.lg.Println("started")
}()
pw.lg.Println("starting...")
chErr <- pw.w.Start(pw.cfg.Watch.Interval)
}()
defer pw.w.Close()
defer func() { _ = pw.kill(ctx) }()
select {
case err := <-chErr:
pw.lg.Printf("error occurred during watch: %s", err)
return err
case <-ctx.Done():
pw.lg.Println("stopping...")
}
return nil
}
type updateHandler func(ctx context.Context, event watcher.Event) error
func (pw *polyWatcher) updateHandler() updateHandler {
var uh func(uu ...update) error
switch pw.cfg.RateLimit.Strategy {
case config.RateLimitStrategyDebounce:
uh, _ = debounce.DebounceWithCustomSignature(pw.handleUpdate, pw.cfg.RateLimit.Wait)
case config.RateLimitStrategyThrottle:
uh, _ = debounce.ThrottleWithCustomSignature(pw.handleUpdate, pw.cfg.RateLimit.Wait)
//case config.RateLimitStrategyAudit:
//case config.RateLimitStrategySample:
default:
uh = pw.handleUpdate
}
return func(ctx context.Context, event watcher.Event) error {
return uh(update{
ctx: ctx,
event: event,
})
}
}
type update struct {
ctx context.Context
event watcher.Event
}
func (pw *polyWatcher) handleUpdate(uu ...update) error {
u := uu[0]
return pw._handleUpdate(u.ctx, u.event)
}
func (pw *polyWatcher) _handleUpdate(ctx context.Context, event watcher.Event) error {
pw.lg.Println("updating...")
err := pw.kill(ctx)
if err != nil {
pw.lg.Printf("unable to kill previous command: %s", err)
}
return pw.cmd.Start()
}
func (pw *polyWatcher) kill(ctx context.Context) error {
if pw.cmd.Process == nil {
return nil
}
if pw.cmd.Process.Pid < 1 {
return nil
}
pw.lg.Printf("killing previous command pid(%d) with sig(%s)", pw.cmd.Process.Pid, pw.cfg.Kill.Signal)
// todo: apply kill timeout
group, err := os.FindProcess(-1 * pw.cmd.Process.Pid)
if err == nil {
err = group.Signal(pw.cfg.Kill.Signal)
if err != nil {
pw.lg.Printf("unable to send signal to previous command: %s", err)
}
}
defer pw.renewCommand()
return handleWaitError(pw.cmd.Wait(), pw.cfg.Kill.Signal)
}
func handleWaitError(err error, sig os.Signal) error {
if e, ok := err.(*exec.ExitError); ok {
status := e.ProcessState.Sys().(syscall.WaitStatus)
if status.Signaled() {
// TODO: seperate windows and posix functionality and compare relevant types
if status.Signal().String() == sig.String() {
return nil
}
}
}
return err
}