diff --git a/manager.go b/manager.go index a217a80..c1a9dd5 100644 --- a/manager.go +++ b/manager.go @@ -2,6 +2,7 @@ package curre import ( "context" + "os" "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 { if m.started { panic("cannot register to a started manager") @@ -37,7 +67,7 @@ func (m *Manager) Register(components ...Component) *Manager { return m } -func (m *Manager) Init(ctx context.Context) error { +func (m *Manager) init(ctx context.Context) error { if m.started { panic("cannot reinit a started manager") } @@ -54,7 +84,7 @@ func (m *Manager) Init(ctx context.Context) error { return nil } -func (m *Manager) Start(ctx context.Context) error { +func (m *Manager) start(ctx context.Context) error { for _, c := range m.components { 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 m.exitCode = exitCode return nil } -func (m *Manager) Shutdown(ctx context.Context) error { +func (m *Manager) shutdown(ctx context.Context) error { shutdownChan := make(chan struct{}, 1) closers := m.getClosers(ctx)