68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
log "github.com/sirupsen/logrus"
|
|
"scrapper-mandarake/config"
|
|
"scrapper-mandarake/internal/scrapper"
|
|
"scrapper-mandarake/internal/tasks"
|
|
"time"
|
|
)
|
|
|
|
const AppName string = "Mandarake parser"
|
|
|
|
type App struct {
|
|
transport tasks.TaskTransport
|
|
scrapper scrapper.PriceScrapper
|
|
}
|
|
|
|
func NewApp(cfg *config.Config) *App {
|
|
a := App{}
|
|
|
|
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")
|
|
|
|
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 {
|
|
log.Info("App shutting down")
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
|
defer cancel()
|
|
|
|
app.scrapper.Stop()
|
|
|
|
_ = ctx
|
|
return nil
|
|
}
|