task handler

This commit is contained in:
nquidox 2026-03-08 14:07:10 +03:00
parent 03bcda8eab
commit 338802339c
3 changed files with 119 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
log "github.com/sirupsen/logrus"
"merch-api/config"
"merch-api/internal/merch"
"merch-api/internal/task"
"merch-api/internal/user"
"merch-api/pkg/dbase"
"merch-api/pkg/router"
@ -21,6 +22,7 @@ type App struct {
router *router.Router
modules []Module
dbPool *pgxpool.Pool
tasker *task.Handler
}
func New(ctx context.Context, cfg config.Config) *App {
@ -34,6 +36,9 @@ func New(ctx context.Context, cfg config.Config) *App {
Password: cfg.DBase.Password,
DBName: cfg.DBase.DBName,
})
if err != nil {
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
}
//providers with deps
userProv := user.New(user.Deps{
@ -49,10 +54,6 @@ func New(ctx context.Context, cfg config.Config) *App {
UserProvider: userProv,
})
if err != nil {
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
}
//modules
var modules []Module
@ -63,11 +64,17 @@ func New(ctx context.Context, cfg config.Config) *App {
})
modules = append(modules, m)
tasker := task.New(task.Deps{
Addr: "",
MerchProvider: m,
})
return &App{
cfg: cfg,
router: r,
modules: modules,
dbPool: dbPool,
tasker: tasker,
}
}
@ -85,6 +92,12 @@ func (app *App) Run(ctx context.Context) error {
}
}()
go func() {
if err := app.tasker.Serve(); err != nil {
errCh <- err
}
}()
select {
case <-ctx.Done():
app.shutdown(ctx)
@ -106,6 +119,8 @@ func (app *App) shutdown(ctx context.Context) {
log.WithError(err).Warnf("%v error shutting down application", pkgLogHeader)
}
app.tasker.Shutdown()
log.Infof("%v shutdown complete", pkgLogHeader)
}