@@ -12,16 +12,17 @@ import (
12
12
type RegistryParams struct {
13
13
fx.In
14
14
fx.Shutdowner
15
- Workers []Worker `group:"workers"`
16
- Logger * zap.SugaredLogger
15
+ Workers []Worker `group:"workers"`
16
+ Decorators []Decorator `group:"worker_decorators"`
17
+ Logger * zap.SugaredLogger
17
18
}
18
19
19
20
type RegistryResult struct {
20
21
fx.Out
21
22
Registry Registry
22
23
}
23
24
24
- func NewRegistry (p RegistryParams ) RegistryResult {
25
+ func NewRegistry (p RegistryParams ) ( RegistryResult , error ) {
25
26
r := & registry {
26
27
mutex : & sync.RWMutex {},
27
28
workers : make (map [string ]Worker ),
@@ -30,7 +31,12 @@ func NewRegistry(p RegistryParams) RegistryResult {
30
31
for _ , w := range p .Workers {
31
32
r .workers [w .Key ()] = w
32
33
}
33
- return RegistryResult {Registry : r }
34
+ for _ , d := range p .Decorators {
35
+ if err := r .decorate (d .Key , d .Decorate ); err != nil {
36
+ return RegistryResult {}, err
37
+ }
38
+ }
39
+ return RegistryResult {Registry : r }, nil
34
40
}
35
41
36
42
type Registry interface {
@@ -41,20 +47,25 @@ type Registry interface {
41
47
DisableAll ()
42
48
Start (ctx context.Context ) error
43
49
Stop (ctx context.Context ) error
44
- Decorate (name string , fn Decorator ) error
50
+ decorate (name string , fn DecorateFunction ) error
45
51
}
46
52
47
53
type Worker interface {
48
54
Key () string
49
55
Enabled () bool
50
56
Started () bool
51
- Decorate (Decorator ) Worker
52
57
_hook () fx.Hook
53
58
setEnabled (enabled bool )
54
59
setStarted (started bool )
60
+ decorate (DecorateFunction ) Worker
55
61
}
56
62
57
- type Decorator func (fx.Hook ) fx.Hook
63
+ type DecorateFunction func (fx.Hook ) fx.Hook
64
+
65
+ type Decorator struct {
66
+ Key string
67
+ Decorate DecorateFunction
68
+ }
58
69
59
70
type worker struct {
60
71
key string
@@ -82,7 +93,7 @@ func (w *worker) Started() bool {
82
93
return w .started
83
94
}
84
95
85
- func (w * worker ) Decorate (fn Decorator ) Worker {
96
+ func (w * worker ) decorate (fn DecorateFunction ) Worker {
86
97
return & worker {
87
98
key : w .key ,
88
99
hook : fn (fx.Hook {
@@ -210,11 +221,11 @@ func (r *registry) Stop(ctx context.Context) error {
210
221
return nil
211
222
}
212
223
213
- func (r * registry ) Decorate (name string , fn Decorator ) error {
224
+ func (r * registry ) decorate (name string , fn DecorateFunction ) error {
214
225
r .mutex .Lock ()
215
226
defer r .mutex .Unlock ()
216
227
if w , ok := r .workers [name ]; ok {
217
- r .workers [name ] = w .Decorate (fn )
228
+ r .workers [name ] = w .decorate (fn )
218
229
return nil
219
230
}
220
231
return fmt .Errorf ("worker %s not found" , name )
0 commit comments