From bcbf567a7ab7a96ede1ddd001e81a8dcc676c67f Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 3 Jun 2023 16:53:20 +0200 Subject: [PATCH] code clean-up; use standardized Run... functions for async calls --- common/common.go | 14 +++++++++++++- scopes.go | 14 ++------------ testing/testing.go | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/common.go b/common/common.go index 22ab41c..60f5d99 100644 --- a/common/common.go +++ b/common/common.go @@ -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 diff --git a/scopes.go b/scopes.go index 61ed059..85f1530 100644 --- a/scopes.go +++ b/scopes.go @@ -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) { diff --git a/testing/testing.go b/testing/testing.go index bf8afd3..0a1700a 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -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 }