-
Notifications
You must be signed in to change notification settings - Fork 25
/
fx-driver.go
199 lines (177 loc) · 6.19 KB
/
fx-driver.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
package notifiers
import (
"context"
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/status"
)
// FxOptionsFunc is a function that returns fx.Option.
type FxOptionsFunc func(Key, config.Unmarshaller, status.Registry) (fx.Option, error)
type fxRunner struct {
statusRegistry status.Registry
fxRunnerStatusRegistry status.Registry
app *fx.App
prometheusRegistry *prometheus.Registry
*unmarshalKeyNotifier
fxOptionsFuncs []FxOptionsFunc
}
// Make sure fxRunner implements KeyNotifier.
var _ KeyNotifier = (*fxRunner)(nil)
func (fr *fxRunner) processEvent(event Event, unmarshaller config.Unmarshaller) {
logger := fr.fxRunnerStatusRegistry.GetLogger()
switch event.Type {
case Write:
logger.Info().Str("event", event.String()).Msg("key update")
if fr.app != nil {
// stop existing app
err := fr.deinitApp()
if err != nil {
logger.Error().Err(err).Msg("Failed to stop existing app")
}
}
// instantiate and start a new app
err := fr.initApp(event.Key, unmarshaller)
if err != nil {
logger.Error().Err(err).Msg("Failed to instantiate and start a new app")
}
case Remove:
logger.Info().Str("event", event.String()).Msg("key removed")
// deinit the app
err := fr.deinitApp()
if err != nil {
logger.Error().Err(err).Msg("Failed to deinit app")
}
fr.statusRegistry.Detach()
}
}
func (fr *fxRunner) initApp(key Key, unmarshaller config.Unmarshaller) error {
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(wrapperspb.String("policy runner initializing"), nil))
logger := fr.fxRunnerStatusRegistry.GetLogger()
if fr.app == nil && unmarshaller != nil {
var options []fx.Option
for _, fxOptionsFunc := range fr.fxOptionsFuncs {
o, e := fxOptionsFunc(key, unmarshaller, fr.statusRegistry)
if e != nil {
logger.Error().Err(e).Msg("fxOptionsFunc failed")
return e
}
options = append(options, o)
}
option := fx.Options(options...)
fr.app = fx.New(
// Note: Supplying unmarshaller directly results in supplying
// concrete type instead of interface, thus supplying via Provide.
fx.Provide(func() config.Unmarshaller { return unmarshaller }),
// Supply keyinfo
fx.Supply(key),
// Supply status registry for the key
fx.Supply(fr.statusRegistry),
// Supply prometheus registry
fx.Supply(fr.prometheusRegistry),
option,
fx.WithLogger(func() fxevent.Logger {
logger := zap.New(
log.NewZapAdapter(log.GetGlobalLogger(), fmt.Sprintf("fxdriver-%s", key.String())),
)
return &fxevent.ZapLogger{Logger: logger}
}),
)
var err error
if err = fr.app.Err(); err != nil {
visualize, _ := fx.VisualizeError(err)
logger.Error().Err(err).Str("visualize", visualize).Msg("fx.New failed")
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(nil, err))
_ = fr.deinitApp()
return err
}
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(wrapperspb.String("policy runner starting"), nil))
ctx, cancel := context.WithTimeout(context.Background(), fr.app.StartTimeout())
defer cancel()
if err = fr.app.Start(ctx); err != nil {
logger.Error().Err(err).Msg("Could not start application")
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(nil, err))
return err
}
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(wrapperspb.String("policy runner started"), nil))
} else {
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(nil, errors.New("fxRunner is not initialized")))
}
return nil
}
func (fr *fxRunner) deinitApp() error {
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(wrapperspb.String("policy runner stopping"), nil))
logger := fr.fxRunnerStatusRegistry.GetLogger()
if fr.app != nil {
ctx, cancel := context.WithTimeout(context.Background(), fr.app.StopTimeout())
defer func() { fr.app = nil }()
defer cancel()
if err := fr.app.Stop(ctx); err != nil {
logger.Error().Err(err).Msg("Could not stop application")
return err
}
}
fr.fxRunnerStatusRegistry.SetStatus(status.NewStatus(wrapperspb.String("policy runner stopped"), nil))
return nil
}
// fxDriver tracks prefix and allows spawning "mini FX-based apps" per key in the prefix.
type fxDriver struct {
statusRegistry status.Registry
prometheusRegistry *prometheus.Registry
// Options for new unmarshaller instances
*unmarshalPrefixNotifier
// function to provide fx.Options.
//
// Resulting fx.Options will be used to create a "mini FX-based apps" per key.
// The lifecycle of the app will be tied to the existence of the key.
// Note that when key's contents change the previous App will be stopped
// and a fresh one will be created.
fxOptionsFuncs []FxOptionsFunc
}
// Make sure FxDriver implements PrefixNotifier.
var _ PrefixNotifier = (*fxDriver)(nil)
// NewFxDriver creates a new FxDriver.
func NewFxDriver(
statusRegistry status.Registry,
prometheusRegistry *prometheus.Registry,
getUnmarshallerFunc GetUnmarshallerFunc,
fxOptionsFuncs []FxOptionsFunc,
) (*fxDriver, error) {
// Subscribe to all prefixes and additional notifier callback is nil
upn, err := NewUnmarshalPrefixNotifier("", nil, getUnmarshallerFunc)
if err != nil {
return nil, err
}
return &fxDriver{
statusRegistry: statusRegistry,
prometheusRegistry: prometheusRegistry,
unmarshalPrefixNotifier: upn,
fxOptionsFuncs: fxOptionsFuncs,
}, nil
}
// GetKeyNotifier returns a KeyNotifier that will notify the driver of key changes.
func (fxd *fxDriver) GetKeyNotifier(key Key) (KeyNotifier, error) {
unmarshaller, err := fxd.getUnmarshallerFunc(nil)
if err != nil {
return nil, err
}
statusRegistry := fxd.statusRegistry.Child("key", key.String())
fr := &fxRunner{
fxOptionsFuncs: fxd.fxOptionsFuncs,
statusRegistry: statusRegistry,
fxRunnerStatusRegistry: statusRegistry.Child("system", "fx_runner"),
prometheusRegistry: fxd.prometheusRegistry,
}
ukn, err := NewUnmarshalKeyNotifier(key, unmarshaller, fr.processEvent)
if err != nil {
return nil, err
}
fr.unmarshalKeyNotifier = ukn
return fr, nil
}