code clean-up; use standardized Run... functions for async calls

This commit is contained in:
Helmut Merz 2023-06-03 16:53:20 +02:00
parent 7d8d9abe54
commit bcbf567a7a
3 changed files with 16 additions and 14 deletions

View file

@ -27,7 +27,19 @@ type Context interface {
WaitGroup() *sync.WaitGroup
}
type StartFct = func(Context)
type FctCtx = func(Context)
type StartFct = FctCtx
// async Runners
func RunCtx(ctx Context, fct FctCtx) {
ctx.WaitGroup().Add(1)
go func() {
defer ctx.WaitGroup().Done()
fct(ctx)
}()
}
// Context implementation

View file

@ -17,27 +17,17 @@ type Cfg struct {
}
func Start(ctx common.Context) {
ctx.WaitGroup().Add(1)
go func() {
start(ctx)
ctx.WaitGroup().Done()
}()
common.RunCtx(ctx, start)
}
func start(ctx common.Context) {
defer close(ctx.Done())
for _, cfg := range ctx.Config().Children() {
cctx := ctx.ChildContext(cfg)
cfg.Starter()(cctx)
fmt.Println(cfg.Name(), " started")
}
fmt.Println("running ", ctx.WaitGroup())
defer func() {
close(ctx.Done())
fmt.Println("finishing #1 ", ctx.WaitGroup())
//ctx.WaitGroup().Wait()
//fmt.Println("finishing #2 ", ctx.WaitGroup())
//ctx.LogDebug("Dispatcher exited", m.Map{})
}()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
for step(ctx, sig) {

View file

@ -14,7 +14,6 @@ import (
func Start(ctx common.Context) {
scopes.Start(ctx)
time.Sleep(100 * time.Millisecond)
}
// definitions
@ -41,6 +40,7 @@ func SetUpApp(tbase *testing.T, cfg common.Config) *T {
t := SetUp(tbase)
t.Ctx = common.AppContext(cfg)
cfg.Starter()(t.Ctx)
time.Sleep(100 * time.Millisecond)
return t
}