with better closer

This commit is contained in:
Kasper Juul Hermansen 2022-09-10 01:39:36 +02:00
parent 6169d05b3e
commit 4214259591
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
3 changed files with 46 additions and 8 deletions

View File

@ -7,9 +7,9 @@ type DefaultLogger struct{}
var _ Logger = &DefaultLogger{}
func (dl *DefaultLogger) Info(msg string, args ...any) {
fmt.Printf(msg, args...)
fmt.Println(fmt.Sprintf(msg, args...))
}
func (dl *DefaultLogger) Error(msg string, args ...any) {
fmt.Printf(msg, args...)
fmt.Println(fmt.Sprintf(msg, args...))
}

33
example/app/main.go Normal file
View File

@ -0,0 +1,33 @@
package main
import (
"context"
"fmt"
"time"
"git.front.kjuulh.io/kjuulh/curre"
)
func main() {
curre.NewManager().
Register(&httpServer{}).
Register(&httpServer{}).
Register(&httpServer{}).
Run(context.Background())
}
type httpServer struct{}
func (hs *httpServer) Init(ctx context.Context) error { return nil }
func (hs *httpServer) Start(ctx context.Context) error {
for {
fmt.Println(fmt.Sprintf("server: %T, %s", hs, time.Now().String()))
time.Sleep(time.Second * 5)
}
return nil
}
func (hs *httpServer) Close(ctx context.Context) error {
return nil
}

View File

@ -23,11 +23,19 @@ type Manager struct {
func NewManager() *Manager {
return &Manager{
logger: &DefaultLogger{}, exitChan: make(chan int, 1),
logger: &DefaultLogger{},
lifetime: ConsoleLifetime,
exitChan: make(chan int, 1),
exitCode: OK,
}
}
func (m *Manager) RunNonBlocking(ctx context.Context) error {
go m.Run(ctx)
return nil
}
func (m *Manager) Run(ctx context.Context) error {
m.initLifetime()
err := m.init(ctx)
@ -50,9 +58,7 @@ func (m *Manager) Run(ctx context.Context) error {
return err
}
if m.exitCode != 0 {
os.Exit(m.exitCode)
}
os.Exit(m.exitCode)
return nil
}
@ -97,9 +103,8 @@ func (m *Manager) startComponent(ctx context.Context, component Component) {
err := recover()
if err != nil {
m.logger.Error("Panic occurred in component: %T, error: %s", component, err)
m.exitChan <- Internal
}
m.exitChan <- Internal
}()
m.logger.Info("Starting %T", component)