root ctx + graceful shutdown
This commit is contained in:
parent
1e2f442781
commit
e104b3e71d
1 changed files with 28 additions and 4 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue