root ctx + graceful shutdown

This commit is contained in:
nquidox 2026-02-18 17:37:37 +03:00
parent 1e2f442781
commit e104b3e71d

View file

@ -1,24 +1,48 @@
package app package app
import ( import (
"context"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"task-processor/config" "task-processor/config"
"time"
) )
type App struct { type App struct {
config config.Config config config.Config
rootCtx context.Context
} }
type Deps struct { type Deps struct {
Config config.Config Config config.Config
RootCtx context.Context
} }
func NewApp(deps Deps) *App { func NewApp(deps Deps) *App {
return &App{ return &App{
config: deps.Config, config: deps.Config,
rootCtx: deps.RootCtx,
} }
} }
func (app *App) Run() { func (app *App) Run() error {
log.Info("App started") log.Info("App started")
errChan := make(chan error, 3)
select {
case <-app.rootCtx.Done():
return app.Shutdown()
case err := <-errChan:
return err
}
}
func (app *App) Shutdown() error {
log.Info("App shutting down")
ctx, cancel := context.WithTimeout(app.rootCtx, time.Second*10)
defer cancel()
_ = ctx
return nil
} }