gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/util/background/manager.go (about) 1 package background 2 3 import ( 4 "context" 5 "sync" 6 ) 7 8 type key struct{} 9 10 type Manager struct{ wg sync.WaitGroup } 11 12 func NotifyWhenCompleted(ctx context.Context) (context.Context, <-chan struct{}) { 13 ch := make(chan struct{}) 14 return context.WithValue(ctx, key{}, ch), ch 15 } 16 17 func (m *Manager) RunBackgroundTask(ctx context.Context, f func(context.Context)) { 18 m.wg.Add(1) 19 go func() { 20 defer m.wg.Done() 21 f(Context(ctx)) 22 23 if done, _ := ctx.Value(key{}).(chan struct{}); done != nil { 24 done <- struct{}{} 25 } 26 }() 27 } 28 29 func (m *Manager) Wait() { m.wg.Wait() }