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