8 Commits

Author SHA1 Message Date
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
3b3c0e6118 as function instead 2022-09-10 19:53:19 +02:00
78c0c309d7 with fc 2022-09-10 13:08:16 +02:00
5 changed files with 86 additions and 9 deletions

34
builder.go Normal file
View File

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

View File

@@ -2,6 +2,6 @@ package curre
import "context" import "context"
type Closer interface { type Stopper interface {
Close(ctx context.Context) error Stop(ctx context.Context) error
} }

View File

@@ -3,5 +3,5 @@ package curre
type Component interface { type Component interface {
Initer Initer
Starter Starter
Closer Stopper
} }

View File

@@ -28,6 +28,6 @@ func (hs *httpServer) Start(ctx context.Context) error {
return nil return nil
} }
func (hs *httpServer) Close(ctx context.Context) error { func (hs *httpServer) Stop(ctx context.Context) error {
return nil return nil
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"sync" "sync"
"time"
) )
const ( const (
@@ -30,9 +31,38 @@ func NewManager() *Manager {
} }
} }
func (m *Manager) RunNonBlocking(ctx context.Context) error { type ComponentsAreReady struct {
go m.Run(ctx) }
func (m *Manager) RunNonBlocking(ctx context.Context, readyChan chan ComponentsAreReady) error {
go func() error {
defer func() {
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err := m.shutdown(ctx)
if err != nil {
panic(err)
}
}()
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 nil
} }
@@ -98,6 +128,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,9 +166,14 @@ func (m *Manager) initLifetime() {
} }
func (m *Manager) wait(ctx context.Context) error { func (m *Manager) wait(ctx context.Context) error {
exitCode := <-m.exitChan select {
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 {
@@ -139,7 +182,7 @@ func (m *Manager) shutdown(ctx context.Context) error {
go func(ctx context.Context) { go func(ctx context.Context) {
for _, c := range closers { for _, c := range closers {
c.Close(ctx) c.Stop(ctx)
} }
shutdownChan <- struct{}{} shutdownChan <- struct{}{}