2026-02-18 15:53:29 +03:00
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-18 17:37:37 +03:00
|
|
|
"context"
|
2026-02-18 15:53:29 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2026-02-18 19:58:32 +03:00
|
|
|
"net"
|
2026-02-18 15:53:29 +03:00
|
|
|
"task-processor/config"
|
2026-02-18 19:58:32 +03:00
|
|
|
ta "task-processor/internal/taskAgent"
|
2026-02-18 17:37:37 +03:00
|
|
|
"time"
|
2026-02-18 15:53:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type App struct {
|
2026-02-18 19:58:32 +03:00
|
|
|
config config.Config
|
|
|
|
|
rootCtx context.Context
|
|
|
|
|
taskAgent ta.TaskAgent
|
2026-02-18 15:53:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Deps struct {
|
2026-02-18 17:37:37 +03:00
|
|
|
Config config.Config
|
|
|
|
|
RootCtx context.Context
|
2026-02-18 15:53:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewApp(deps Deps) *App {
|
2026-02-18 19:58:32 +03:00
|
|
|
cfg := deps.Config
|
|
|
|
|
|
|
|
|
|
taskAgent := ta.NewHandler(ta.Deps{
|
|
|
|
|
Addr: net.JoinHostPort(cfg.TasksSource.Host, cfg.TasksSource.Port),
|
|
|
|
|
Timeout: cfg.TasksSource.Timeout,
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-18 15:53:29 +03:00
|
|
|
return &App{
|
2026-02-18 19:58:32 +03:00
|
|
|
config: cfg,
|
|
|
|
|
rootCtx: deps.RootCtx,
|
|
|
|
|
taskAgent: taskAgent,
|
2026-02-18 15:53:29 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 17:37:37 +03:00
|
|
|
func (app *App) Run() error {
|
2026-02-18 15:53:29 +03:00
|
|
|
log.Info("App started")
|
2026-02-18 17:37:37 +03:00
|
|
|
|
|
|
|
|
errChan := make(chan error, 3)
|
|
|
|
|
|
2026-02-18 19:58:32 +03:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-02-18 17:37:37 +03:00
|
|
|
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
|
2026-02-18 15:53:29 +03:00
|
|
|
}
|
2026-02-18 19:58:32 +03:00
|
|
|
|
|
|
|
|
func (app *App) processTasks() error {
|
|
|
|
|
log.Info("Processing tasks")
|
|
|
|
|
return nil
|
|
|
|
|
}
|