NewContextWithCause is like NewContext but returns a context.CancelCauseFunc. EXPERIMENTAL: This API is subject to change.
(ctx Context)
| 70 | // NewContextWithCause is like NewContext but returns a context.CancelCauseFunc. |
| 71 | // EXPERIMENTAL: This API is subject to change. |
| 72 | func NewContextWithCause(ctx Context) (Context, context.CancelCauseFunc) { |
| 73 | newCtx := Context{moduleInstances: make(map[string][]Module), cfg: ctx.cfg, metricsRegistry: prometheus.NewPedanticRegistry()} |
| 74 | c, cancel := context.WithCancelCause(ctx.Context) |
| 75 | wrappedCancel := func(cause error) { |
| 76 | cancel(cause) |
| 77 | |
| 78 | for _, f := range ctx.cleanupFuncs { |
| 79 | f() |
| 80 | } |
| 81 | |
| 82 | for modName, modInstances := range newCtx.moduleInstances { |
| 83 | for _, inst := range modInstances { |
| 84 | if cu, ok := inst.(CleanerUpper); ok { |
| 85 | err := cu.Cleanup() |
| 86 | if err != nil { |
| 87 | log.Printf("[ERROR] %s (%p): cleanup: %v", modName, inst, err) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | newCtx.Context = c |
| 94 | newCtx.initMetrics() |
| 95 | return newCtx, wrappedCancel |
| 96 | } |
| 97 | |
| 98 | // OnCancel executes f when ctx is canceled. |
| 99 | func (ctx *Context) OnCancel(f func()) { |
no test coverage detected