8 Commits

Author SHA1 Message Date
47f249af82 with manager 2022-09-21 22:24:24 +02:00
a59a278140 with callback instead 2022-09-21 22:19:54 +02:00
4774158f24 with stop 2022-09-21 22:14:22 +02:00
5f89d83094 with defer 2022-09-21 22:08:27 +02:00
f9109382cb components signal 2022-09-21 21:45:48 +02:00
eb66692b47 With actual nonblocking one by one manager 2022-09-21 21:40:44 +02:00
44218ec4ac with state in funcs 2022-09-10 20:05:10 +02:00
a2c53271e4 with public func 2022-09-10 19:55:19 +02:00
2 changed files with 54 additions and 14 deletions

View File

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

View File

@@ -30,10 +30,37 @@ func NewManager() *Manager {
} }
} }
func (m *Manager) RunNonBlocking(ctx context.Context) error { type ComponentsAreReady struct {
go m.Run(ctx) }
return nil 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 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 {
@@ -98,6 +125,14 @@ 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()
@@ -128,8 +163,13 @@ func (m *Manager) initLifetime() {
} }
func (m *Manager) wait(ctx context.Context) error { func (m *Manager) wait(ctx context.Context) error {
exitCode := <-m.exitChan select {
m.exitCode = exitCode case exitCode := <-m.exitChan:
m.exitCode = exitCode
return nil
case <-ctx.Done():
return nil
}
return nil return nil
} }