use special Quit message for stopping app; check Done channel

This commit is contained in:
Helmut Merz 2023-06-06 08:36:01 +02:00
parent 22e8759369
commit 35799f8d6c
4 changed files with 22 additions and 8 deletions

View file

@ -8,6 +8,7 @@ import (
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/message"
) )
type Cfg struct { type Cfg struct {
@ -41,13 +42,15 @@ func step(ctx lib.Context, sig <-chan os.Signal) bool {
//ctx.LogInfo("Dispatcher interrupted", m.Map{}) //ctx.LogInfo("Dispatcher interrupted", m.Map{})
return false return false
case msg := <-ctx.Mailbox(): case msg := <-ctx.Mailbox():
fmt.Println("message", msg) fmt.Println("app.step: message =", msg)
//ctx.LogDebug("dispatcherStep", m.Map{"msg": msg}) //ctx.LogDebug("dispatcherStep", m.Map{"msg": msg})
if msg.Action() == "quit" { if msg == message.Quit {
//ctx.LogInfo("Dispatcher stopped", m.Map{}) //ctx.LogInfo("Dispatcher stopped", m.Map{})
return false return false
} }
return ctx.HandleMsg(msg) return ctx.HandleMsg(msg)
case <-ctx.Done():
return false
} }
return true return true
} }

View file

@ -26,6 +26,8 @@ type Base struct {
children []lib.Config children []lib.Config
} }
// lib.Config implementation
func (cfg *Base) Name() string { func (cfg *Base) Name() string {
return cfg.name return cfg.name
} }
@ -42,11 +44,6 @@ func (cfg *Base) MessageHandler() lib.MessageHandler {
return cfg.msgHandler return cfg.msgHandler
} }
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
cfg.msgHandler = hdlr
return cfg
}
func (cfg *Base) Actions() []lib.ActionConfig { func (cfg *Base) Actions() []lib.ActionConfig {
return cfg.actions return cfg.actions
} }
@ -59,6 +56,18 @@ func (cfg *Base) Add(child lib.Config) {
cfg.children = append(cfg.children, child) cfg.children = append(cfg.children, child)
} }
// implementation-specific methods and functions
func (cfg *Base) WithStep(step lib.StepProc) *Base {
cfg.step = step
return cfg
}
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
cfg.msgHandler = hdlr
return cfg
}
func MakeBase(name string, starter lib.StartProc) *Base { func MakeBase(name string, starter lib.StartProc) *Base {
return &Base{ return &Base{
name: name, name: name,

View file

@ -18,6 +18,8 @@ func StrMessage(action string) strMessage {
return strMessage(action) return strMessage(action)
} }
var Quit = StrMessage("quit")
// Address implementation // Address implementation
type address struct { type address struct {

View file

@ -50,7 +50,7 @@ func (t *T) TearDownApp() {
// give actors time to recieve all messages: // give actors time to recieve all messages:
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
//t.Check() //t.Check()
t.Ctx.Services()["testing"].Mailbox() <- message.StrMessage("quit") t.Ctx.Services()["testing"].Mailbox() <- message.Quit
fmt.Println("teardown ", t.Ctx.WaitGroup()) fmt.Println("teardown ", t.Ctx.WaitGroup())
t.Ctx.WaitGroup().Wait() t.Ctx.WaitGroup().Wait()
//t.AssertNoUncheckedMessages() //t.AssertNoUncheckedMessages()