diff --git a/internal/app/handler.go b/internal/app/handler.go index c481c6d..b7b56f0 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -3,35 +3,57 @@ package app import ( "context" log "github.com/sirupsen/logrus" - "parser-mandarake/config" + "scrapper-mandarake/config" + "scrapper-mandarake/internal/scrapper" + "scrapper-mandarake/internal/tasks" "time" ) const AppName string = "Mandarake parser" type App struct { - mode string + transport tasks.TaskTransport + scrapper scrapper.PriceScrapper } func NewApp(cfg *config.Config) *App { - a := App{ - mode: cfg.App.Mode, - } + a := App{} - log.Infof("%v: %v", AppName, a.mode) + a.transport = tasks.New(tasks.Deps{ + Username: cfg.Rabbit.User, + Password: cfg.Rabbit.Pass, + Host: cfg.Rabbit.Host, + Port: cfg.Rabbit.Port, + Vhost: cfg.Rabbit.Vhost, + TaskSourceQueue: cfg.Rabbit.TaskSourceQueue, + TaskResultQueue: cfg.Rabbit.TaskResultQueue, + LoggingEnabled: cfg.Rabbit.LoggingEnabled, + ChanLen: cfg.App.ChanLen, + }) + + a.scrapper = scrapper.New(scrapper.Deps{ + ExternalBrowser: cfg.App.ExternalBrowser, + GoroutinesNumber: cfg.App.NumCPUs, + TaskTimeout: cfg.App.TaskTimeoutSeconds, + }) + + log.Infof("%v: %v", AppName, cfg.App.Mode) return &a } func (app *App) Run(ctx context.Context) error { log.Info("App started") - errChan := make(chan error, 3) - select { - case <-ctx.Done(): - return app.Shutdown(ctx) - case err := <-errChan: + getTasksChan := app.transport.GetTasks(ctx) + sendResultsChan := app.transport.SendResult(ctx) + + if err := app.scrapper.Start(ctx, getTasksChan, sendResultsChan); err != nil { + log.Fatal(err) return err } + + <-ctx.Done() + return app.Shutdown(ctx) } func (app *App) Shutdown(ctx context.Context) error { @@ -39,6 +61,8 @@ func (app *App) Shutdown(ctx context.Context) error { ctx, cancel := context.WithTimeout(ctx, time.Second*10) defer cancel() + app.scrapper.Stop() + _ = ctx return nil }