-
Notifications
You must be signed in to change notification settings - Fork 491
/
worker.go
123 lines (104 loc) · 3.02 KB
/
worker.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
// Copyright 2023 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package filenotifywatcher
import (
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/worker/v3"
"github.com/juju/worker/v3/catacomb"
)
// FileNotifyWatcher represents a way to watch for changes in a namespace folder
// directory.
type FileNotifyWatcher interface {
// Changes returns a channel for the given file name that will contain
// if a file was created or deleted.
// TODO (stickupkid): We could further advance this to return a channel
// of ints that represents the number of changes we want to advance per
// step.
Changes(fileName string) (<-chan bool, error)
}
// WorkerConfig encapsulates the configuration options for the
// changestream worker.
type WorkerConfig struct {
Clock clock.Clock
Logger Logger
NewWatcher WatcherFn
NewINotifyWatcher func() (INotifyWatcher, error)
}
// Validate ensures that the config values are valid.
func (c *WorkerConfig) Validate() error {
if c.Clock == nil {
return errors.NotValidf("missing Clock")
}
if c.Logger == nil {
return errors.NotValidf("missing Logger")
}
if c.NewWatcher == nil {
return errors.NotValidf("missing NewWatcher")
}
return nil
}
type fileNotifyWorker struct {
cfg WorkerConfig
catacomb catacomb.Catacomb
runner *worker.Runner
}
func newWorker(cfg WorkerConfig) (*fileNotifyWorker, error) {
var err error
if err = cfg.Validate(); err != nil {
return nil, errors.Trace(err)
}
w := &fileNotifyWorker{
cfg: cfg,
runner: worker.NewRunner(worker.RunnerParams{
// Prevent the runner from restarting the worker, if one of the
// workers dies, we want to stop the whole thing.
IsFatal: func(err error) bool { return false },
Clock: cfg.Clock,
}),
}
if err = catacomb.Invoke(catacomb.Plan{
Site: &w.catacomb,
Work: w.loop,
Init: []worker.Worker{
w.runner,
},
}); err != nil {
return nil, errors.Trace(err)
}
return w, nil
}
func (w *fileNotifyWorker) loop() (err error) {
defer w.runner.Kill()
for {
select {
case <-w.catacomb.Dying():
return w.catacomb.ErrDying()
}
}
}
// Kill is part of the worker.Worker interface.
func (w *fileNotifyWorker) Kill() {
w.catacomb.Kill(nil)
}
// Wait is part of the worker.Worker interface.
func (w *fileNotifyWorker) Wait() error {
return w.catacomb.Wait()
}
// Changes returns a channel containing all the change events for the given
// fileName.
func (w *fileNotifyWorker) Changes(fileName string) (<-chan bool, error) {
if fw, err := w.runner.Worker(fileName, w.catacomb.Dying()); err == nil {
return fw.(FileWatcher).Changes(), nil
}
watcher, err := w.cfg.NewWatcher(fileName, WithLogger(w.cfg.Logger), WithINotifyWatcherFn(w.cfg.NewINotifyWatcher))
if err != nil {
return nil, errors.Trace(err)
}
if err := w.runner.StartWorker(fileName, func() (worker.Worker, error) {
return watcher, nil
}); err != nil {
return nil, errors.Annotatef(err, "starting worker for fileName %q", fileName)
}
return watcher.Changes(), nil
}