run processor
This commit is contained in:
parent
9c79b04d4c
commit
c44709373e
2 changed files with 36 additions and 32 deletions
|
|
@ -17,12 +17,9 @@ func main() {
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
appl := app.NewApp(app.Deps{
|
appl := app.NewApp(ctx, c)
|
||||||
Config: c,
|
|
||||||
RootCtx: ctx,
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := appl.Run(); err != nil {
|
if err := appl.Run(ctx); err != nil {
|
||||||
log.WithError(err).Fatal("Application run failed")
|
log.WithError(err).Fatal("Application run failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,75 +5,82 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"net"
|
"net"
|
||||||
"task-processor/config"
|
"task-processor/config"
|
||||||
|
"task-processor/internal/processor"
|
||||||
ta "task-processor/internal/taskAgent"
|
ta "task-processor/internal/taskAgent"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
config config.Config
|
config config.Config
|
||||||
rootCtx context.Context
|
|
||||||
taskAgent ta.TaskAgent
|
taskAgent ta.TaskAgent
|
||||||
|
processor processor.Processor
|
||||||
|
sendChanLen uint
|
||||||
}
|
}
|
||||||
|
|
||||||
type Deps struct {
|
func NewApp(ctx context.Context, cfg config.Config) *App {
|
||||||
Config config.Config
|
|
||||||
RootCtx context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApp(deps Deps) *App {
|
|
||||||
cfg := deps.Config
|
|
||||||
|
|
||||||
taskAgent := ta.NewHandler(ta.Deps{
|
taskAgent := ta.NewHandler(ta.Deps{
|
||||||
Addr: net.JoinHostPort(cfg.TasksSource.Host, cfg.TasksSource.Port),
|
Addr: net.JoinHostPort(cfg.TasksSource.Host, cfg.TasksSource.Port),
|
||||||
Timeout: cfg.TasksSource.Timeout,
|
Timeout: cfg.TasksSource.Timeout,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
proc := processor.NewHandler(processor.Deps{
|
||||||
|
Ctx: ctx,
|
||||||
|
TA: taskAgent,
|
||||||
|
Addr: processor.Addr{
|
||||||
|
Host: cfg.Rabbit.Host,
|
||||||
|
Port: cfg.Rabbit.Port,
|
||||||
|
User: cfg.Rabbit.User,
|
||||||
|
Pass: cfg.Rabbit.Pass,
|
||||||
|
Vhost: cfg.Rabbit.Vhost,
|
||||||
|
},
|
||||||
|
ChanLen: cfg.App.ProcChanLen,
|
||||||
|
})
|
||||||
|
|
||||||
return &App{
|
return &App{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
rootCtx: deps.RootCtx,
|
|
||||||
taskAgent: taskAgent,
|
taskAgent: taskAgent,
|
||||||
|
processor: proc,
|
||||||
|
sendChanLen: cfg.App.ProcChanLen,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Run() error {
|
func (app *App) Run(ctx context.Context) error {
|
||||||
log.Info("App started")
|
log.Info("App started")
|
||||||
|
|
||||||
errChan := make(chan error, 3)
|
errChan := make(chan error, 3)
|
||||||
|
|
||||||
mainLoop := time.NewTicker(app.config.App.CheckPeriod)
|
mainLoop := time.NewTicker(app.config.App.CheckPeriod)
|
||||||
defer mainLoop.Stop()
|
defer mainLoop.Stop()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := app.processTasks(); err != nil {
|
if err := app.processor.ProcessTasks(ctx); err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
}
|
}
|
||||||
|
|
||||||
for range mainLoop.C {
|
for range mainLoop.C {
|
||||||
if err := app.processTasks(); err != nil {
|
if err := app.processor.ProcessTasks(ctx); err != nil {
|
||||||
errChan <- err
|
errChan <- err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if err := app.processor.SendResults(ctx, app.sendChanLen); err != nil {
|
||||||
|
errChan <- err
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-app.rootCtx.Done():
|
case <-ctx.Done():
|
||||||
return app.Shutdown()
|
return app.Shutdown(ctx)
|
||||||
|
|
||||||
case err := <-errChan:
|
case err := <-errChan:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Shutdown() error {
|
func (app *App) Shutdown(ctx context.Context) error {
|
||||||
log.Info("App shutting down")
|
log.Info("App shutting down")
|
||||||
ctx, cancel := context.WithTimeout(app.rootCtx, time.Second*10)
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
_ = ctx
|
_ = ctx
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) processTasks() error {
|
|
||||||
log.Info("Processing tasks")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue