Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
eb66692b47
|
|||
44218ec4ac
|
|||
a2c53271e4
|
|||
3b3c0e6118
|
|||
78c0c309d7
|
34
builder.go
Normal file
34
builder.go
Normal 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
|
||||
}
|
@@ -2,6 +2,6 @@ package curre
|
||||
|
||||
import "context"
|
||||
|
||||
type Closer interface {
|
||||
Close(ctx context.Context) error
|
||||
type Stopper interface {
|
||||
Stop(ctx context.Context) error
|
||||
}
|
||||
|
@@ -3,5 +3,5 @@ package curre
|
||||
type Component interface {
|
||||
Initer
|
||||
Starter
|
||||
Closer
|
||||
Stopper
|
||||
}
|
||||
|
@@ -28,6 +28,6 @@ func (hs *httpServer) Start(ctx context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
func (hs *httpServer) Close(ctx context.Context) error {
|
||||
func (hs *httpServer) Stop(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
35
manager.go
35
manager.go
@@ -30,9 +30,30 @@ func NewManager() *Manager {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RunNonBlocking(ctx context.Context) error {
|
||||
go m.Run(ctx)
|
||||
type ComponentsAreReady struct {
|
||||
}
|
||||
|
||||
func (m *Manager) RunNonBlocking(ctx context.Context) error {
|
||||
m.initLifetime()
|
||||
err := m.init(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.startBlocking(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.wait(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = m.shutdown(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -98,6 +119,14 @@ func (m *Manager) start(ctx context.Context) error {
|
||||
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) {
|
||||
defer func() {
|
||||
err := recover()
|
||||
@@ -139,7 +168,7 @@ func (m *Manager) shutdown(ctx context.Context) error {
|
||||
|
||||
go func(ctx context.Context) {
|
||||
for _, c := range closers {
|
||||
c.Close(ctx)
|
||||
c.Stop(ctx)
|
||||
}
|
||||
|
||||
shutdownChan <- struct{}{}
|
||||
|
Reference in New Issue
Block a user