From 421425959197c5dd3e683e985438db116041ba4f Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 10 Sep 2022 01:39:36 +0200 Subject: [PATCH] with better closer --- default_logger.go | 4 ++-- example/app/main.go | 33 +++++++++++++++++++++++++++++++++ manager.go | 17 +++++++++++------ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 example/app/main.go diff --git a/default_logger.go b/default_logger.go index aacd3de..4267be5 100644 --- a/default_logger.go +++ b/default_logger.go @@ -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...)) } diff --git a/example/app/main.go b/example/app/main.go new file mode 100644 index 0000000..3a312d5 --- /dev/null +++ b/example/app/main.go @@ -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 +} diff --git a/manager.go b/manager.go index c1a9dd5..96a653e 100644 --- a/manager.go +++ b/manager.go @@ -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)