Compare commits

..

No commits in common. "a59a2781409cfa699f0d6b132b4b43da739b926c" and "a2c53271e4c817a2eb05e8b55e17f3887dba6f71" have entirely different histories.

2 changed files with 11 additions and 52 deletions

View File

@ -3,9 +3,9 @@ package curre
import "context" import "context"
type FunctionalComponent struct { type FunctionalComponent struct {
InitFunc func(fc *FunctionalComponent, ctx context.Context) error InitFunc func(ctx context.Context) error
StartFunc func(fc *FunctionalComponent, ctx context.Context) error StartFunc func(ctx context.Context) error
StopFunc func(fc *FunctionalComponent, ctx context.Context) error StopFunc func(ctx context.Context) error
} }
func NewFunctionalComponent( func NewFunctionalComponent(
@ -16,19 +16,19 @@ func NewFunctionalComponent(
func (fc *FunctionalComponent) Init(ctx context.Context) error { func (fc *FunctionalComponent) Init(ctx context.Context) error {
if fc.InitFunc != nil { if fc.InitFunc != nil {
return fc.InitFunc(fc, ctx) return fc.InitFunc(ctx)
} }
return nil return nil
} }
func (fc *FunctionalComponent) Start(ctx context.Context) error { func (fc *FunctionalComponent) Start(ctx context.Context) error {
if fc.StartFunc != nil { if fc.StartFunc != nil {
return fc.StartFunc(fc, ctx) return fc.StartFunc(ctx)
} }
return nil return nil
} }
func (fc *FunctionalComponent) Stop(ctx context.Context) error { func (fc *FunctionalComponent) Stop(ctx context.Context) error {
if fc.StopFunc != nil { if fc.StopFunc != nil {
return fc.StopFunc(fc, ctx) return fc.StopFunc(ctx)
} }
return nil return nil
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"os" "os"
"sync" "sync"
"time"
) )
const ( const (
@ -31,37 +30,10 @@ func NewManager() *Manager {
} }
} }
type ComponentsAreReady struct { func (m *Manager) RunNonBlocking(ctx context.Context) error {
} go m.Run(ctx)
type CleanupFunc func(ctx context.Context) error
func (m *Manager) RunNonBlocking(ctx context.Context, readyChan chan ComponentsAreReady) (CleanupFunc, error) {
go func() error {
m.initLifetime()
err := m.init(ctx)
if err != nil {
return err
}
err = m.startBlocking(ctx)
if err != nil {
return err
}
readyChan <- ComponentsAreReady{}
err = m.wait(ctx)
if err != nil {
return err
}
return nil return nil
}()
return func(ctx context.Context) error {
return m.shutdown(ctx)
}, nil
} }
func (m *Manager) Run(ctx context.Context) error { func (m *Manager) Run(ctx context.Context) error {
@ -126,14 +98,6 @@ func (m *Manager) start(ctx context.Context) error {
return nil return nil
} }
func (m *Manager) startBlocking(ctx context.Context) error {
for _, c := range m.components {
m.startComponent(ctx, c)
}
return nil
}
func (m *Manager) startComponent(ctx context.Context, component Component) { func (m *Manager) startComponent(ctx context.Context, component Component) {
defer func() { defer func() {
err := recover() err := recover()
@ -164,14 +128,9 @@ func (m *Manager) initLifetime() {
} }
func (m *Manager) wait(ctx context.Context) error { func (m *Manager) wait(ctx context.Context) error {
select { exitCode := <-m.exitChan
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 {