| 1 | package helpers |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "go.uber.org/fx" |
| 7 | ) |
| 8 | |
| 9 | type MetricsCtx context.Context |
| 10 | |
| 11 | // LifecycleCtx creates a context which will be canceled when lifecycle stops |
| 12 | // |
| 13 | // This is a hack which we need because most of our services use contexts in a |
| 14 | // wrong way |
| 15 | func LifecycleCtx(mctx MetricsCtx, lc fx.Lifecycle) context.Context { |
| 16 | ctx, cancel := context.WithCancel(mctx) |
| 17 | lc.Append(fx.Hook{ |
| 18 | OnStop: func(_ context.Context) error { |
| 19 | cancel() |
| 20 | return nil |
| 21 | }, |
| 22 | }) |
| 23 | return ctx |
| 24 | } |