From e104b3e71d97f992110cca4febae4a985995354a Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 18 Feb 2026 17:37:37 +0300 Subject: [PATCH] root ctx + graceful shutdown --- internal/app/handler.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/internal/app/handler.go b/internal/app/handler.go index b2ced79..5f6cca2 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -1,24 +1,48 @@ package app import ( + "context" log "github.com/sirupsen/logrus" "task-processor/config" + "time" ) type App struct { - config config.Config + config config.Config + rootCtx context.Context } type Deps struct { - Config config.Config + Config config.Config + RootCtx context.Context } func NewApp(deps Deps) *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") + + 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 }