with stop

This commit is contained in:
Kasper Juul Hermansen 2022-09-21 22:14:22 +02:00
parent 5f89d83094
commit 4774158f24
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"sync" "sync"
"time"
) )
const ( const (
@ -35,12 +36,13 @@ type ComponentsAreReady struct {
func (m *Manager) RunNonBlocking(ctx context.Context, readyChan chan ComponentsAreReady) error { func (m *Manager) RunNonBlocking(ctx context.Context, readyChan chan ComponentsAreReady) error {
go func() error { go func() error {
defer func(ctx context.Context) { defer func() {
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err := m.shutdown(ctx) err := m.shutdown(ctx)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}(ctx) }()
m.initLifetime() m.initLifetime()
err := m.init(ctx) err := m.init(ctx)
if err != nil { if err != nil {
@ -164,9 +166,14 @@ func (m *Manager) initLifetime() {
} }
func (m *Manager) wait(ctx context.Context) error { func (m *Manager) wait(ctx context.Context) error {
exitCode := <-m.exitChan select {
case exitCode := <-m.exitChan:
m.exitCode = exitCode m.exitCode = exitCode
return nil return nil
case <-ctx.Done():
return nil
}
return nil
} }
func (m *Manager) shutdown(ctx context.Context) error { func (m *Manager) shutdown(ctx context.Context) error {