Compare commits
7 Commits
v1.2.1
...
db85184dca
Author | SHA1 | Date | |
---|---|---|---|
db85184dca
|
|||
a59a278140
|
|||
4774158f24
|
|||
5f89d83094
|
|||
f9109382cb
|
|||
eb66692b47
|
|||
44218ec4ac
|
9
.drone.yml
Executable file
9
.drone.yml
Executable file
@@ -0,0 +1,9 @@
|
||||
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: "test"
|
||||
steps:
|
||||
- name: test
|
||||
image: bash
|
||||
commands:
|
||||
- echo 'Run tests'
|
12
builder.go
12
builder.go
@@ -3,9 +3,9 @@ package curre
|
||||
import "context"
|
||||
|
||||
type FunctionalComponent struct {
|
||||
InitFunc func(ctx context.Context) error
|
||||
StartFunc func(ctx context.Context) error
|
||||
StopFunc func(ctx context.Context) error
|
||||
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(
|
||||
@@ -16,19 +16,19 @@ func NewFunctionalComponent(
|
||||
|
||||
func (fc *FunctionalComponent) Init(ctx context.Context) error {
|
||||
if fc.InitFunc != nil {
|
||||
return fc.InitFunc(ctx)
|
||||
return fc.InitFunc(fc, ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (fc *FunctionalComponent) Start(ctx context.Context) error {
|
||||
if fc.StartFunc != nil {
|
||||
return fc.StartFunc(ctx)
|
||||
return fc.StartFunc(fc, ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (fc *FunctionalComponent) Stop(ctx context.Context) error {
|
||||
if fc.StopFunc != nil {
|
||||
return fc.StopFunc(ctx)
|
||||
return fc.StopFunc(fc, ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
51
manager.go
51
manager.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,10 +31,37 @@ func NewManager() *Manager {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) RunNonBlocking(ctx context.Context) error {
|
||||
go m.Run(ctx)
|
||||
type ComponentsAreReady struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -98,6 +126,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()
|
||||
@@ -128,8 +164,13 @@ func (m *Manager) initLifetime() {
|
||||
}
|
||||
|
||||
func (m *Manager) wait(ctx context.Context) error {
|
||||
exitCode := <-m.exitChan
|
||||
m.exitCode = exitCode
|
||||
select {
|
||||
case exitCode := <-m.exitChan:
|
||||
m.exitCode = exitCode
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user