60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"git.sr.ht/~cco/go-scopes/config"
|
|
"git.sr.ht/~cco/go-scopes/lib"
|
|
"git.sr.ht/~cco/go-scopes/lib/message"
|
|
"git.sr.ht/~cco/go-scopes/logging"
|
|
"git.sr.ht/~cco/go-scopes/logging/log"
|
|
)
|
|
|
|
type Cfg struct {
|
|
*config.BaseCfg
|
|
AppType string
|
|
Home string
|
|
Logging *logging.Config
|
|
}
|
|
|
|
func Start(ctx lib.Context) {
|
|
lib.RunCtx(ctx, start)
|
|
}
|
|
|
|
func start(ctx lib.Context) {
|
|
defer ctx.Stop()
|
|
for _, cfg := range ctx.Config().Children() {
|
|
cctx := ctx.ChildContext(cfg)
|
|
cfg.Starter()(cctx)
|
|
fmt.Println(cfg.Name(), " started")
|
|
log.Info().Str("service", cfg.Name()).Msg("started")
|
|
}
|
|
fmt.Println("running ", ctx.WaitGroup())
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
|
for step(ctx, sig) {
|
|
}
|
|
}
|
|
|
|
func step(ctx lib.Context, sig <-chan os.Signal) bool {
|
|
select {
|
|
case <-sig:
|
|
fmt.Println("interrupted")
|
|
//ctx.LogInfo("Dispatcher interrupted", m.Map{})
|
|
return false
|
|
case msg := <-ctx.Mailbox():
|
|
fmt.Println("app.step: message =", msg)
|
|
//ctx.LogDebug("dispatcherStep", m.Map{"msg": msg})
|
|
if msg == message.Quit {
|
|
//ctx.LogInfo("Dispatcher stopped", m.Map{})
|
|
return false
|
|
}
|
|
return lib.HandleMsg(ctx, msg)
|
|
case <-ctx.Done():
|
|
return false
|
|
}
|
|
return true
|
|
}
|