task-processor/internal/app/handler.go
2026-02-18 19:58:32 +03:00

79 lines
1.3 KiB
Go

package app
import (
"context"
log "github.com/sirupsen/logrus"
"net"
"task-processor/config"
ta "task-processor/internal/taskAgent"
"time"
)
type App struct {
config config.Config
rootCtx context.Context
taskAgent ta.TaskAgent
}
type Deps struct {
Config config.Config
RootCtx context.Context
}
func NewApp(deps Deps) *App {
cfg := deps.Config
taskAgent := ta.NewHandler(ta.Deps{
Addr: net.JoinHostPort(cfg.TasksSource.Host, cfg.TasksSource.Port),
Timeout: cfg.TasksSource.Timeout,
})
return &App{
config: cfg,
rootCtx: deps.RootCtx,
taskAgent: taskAgent,
}
}
func (app *App) Run() error {
log.Info("App started")
errChan := make(chan error, 3)
mainLoop := time.NewTicker(app.config.App.CheckPeriod)
defer mainLoop.Stop()
go func() {
if err := app.processTasks(); err != nil {
errChan <- err
}
for range mainLoop.C {
if err := app.processTasks(); err != nil {
errChan <- err
}
}
}()
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
}
func (app *App) processTasks() error {
log.Info("Processing tasks")
return nil
}