This commit is contained in:
Kasper Juul Hermansen 2022-09-10 01:25:07 +02:00
parent b8fcd14ebe
commit 6169d05b3e
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -2,6 +2,7 @@ package curre
import ( import (
"context" "context"
"os"
"sync" "sync"
) )
@ -27,6 +28,35 @@ func NewManager() *Manager {
} }
} }
func (m *Manager) Run(ctx context.Context) error {
m.initLifetime()
err := m.init(ctx)
if err != nil {
return err
}
err = m.start(ctx)
if err != nil {
return err
}
err = m.wait(ctx)
if err != nil {
return err
}
err = m.shutdown(ctx)
if err != nil {
return err
}
if m.exitCode != 0 {
os.Exit(m.exitCode)
}
return nil
}
func (m *Manager) Register(components ...Component) *Manager { func (m *Manager) Register(components ...Component) *Manager {
if m.started { if m.started {
panic("cannot register to a started manager") panic("cannot register to a started manager")
@ -37,7 +67,7 @@ func (m *Manager) Register(components ...Component) *Manager {
return m return m
} }
func (m *Manager) Init(ctx context.Context) error { func (m *Manager) init(ctx context.Context) error {
if m.started { if m.started {
panic("cannot reinit a started manager") panic("cannot reinit a started manager")
} }
@ -54,7 +84,7 @@ func (m *Manager) Init(ctx context.Context) error {
return nil return nil
} }
func (m *Manager) Start(ctx context.Context) error { func (m *Manager) start(ctx context.Context) error {
for _, c := range m.components { for _, c := range m.components {
go m.startComponent(ctx, c) go m.startComponent(ctx, c)
} }
@ -92,13 +122,13 @@ func (m *Manager) initLifetime() {
}() }()
} }
func (m *Manager) Wait(ctx context.Context) error { func (m *Manager) wait(ctx context.Context) error {
exitCode := <-m.exitChan exitCode := <-m.exitChan
m.exitCode = exitCode m.exitCode = exitCode
return nil return nil
} }
func (m *Manager) Shutdown(ctx context.Context) error { func (m *Manager) shutdown(ctx context.Context) error {
shutdownChan := make(chan struct{}, 1) shutdownChan := make(chan struct{}, 1)
closers := m.getClosers(ctx) closers := m.getClosers(ctx)