init package + start
This commit is contained in:
parent
8922b8a4f0
commit
099b03a862
1 changed files with 35 additions and 11 deletions
|
|
@ -3,35 +3,57 @@ package app
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"parser-mandarake/config"
|
"scrapper-mandarake/config"
|
||||||
|
"scrapper-mandarake/internal/scrapper"
|
||||||
|
"scrapper-mandarake/internal/tasks"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const AppName string = "Mandarake parser"
|
const AppName string = "Mandarake parser"
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
mode string
|
transport tasks.TaskTransport
|
||||||
|
scrapper scrapper.PriceScrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(cfg *config.Config) *App {
|
func NewApp(cfg *config.Config) *App {
|
||||||
a := App{
|
a := App{}
|
||||||
mode: cfg.App.Mode,
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
return &a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Run(ctx context.Context) error {
|
func (app *App) Run(ctx context.Context) error {
|
||||||
log.Info("App started")
|
log.Info("App started")
|
||||||
errChan := make(chan error, 3)
|
|
||||||
|
|
||||||
select {
|
getTasksChan := app.transport.GetTasks(ctx)
|
||||||
case <-ctx.Done():
|
sendResultsChan := app.transport.SendResult(ctx)
|
||||||
return app.Shutdown(ctx)
|
|
||||||
case err := <-errChan:
|
if err := app.scrapper.Start(ctx, getTasksChan, sendResultsChan); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<-ctx.Done()
|
||||||
|
return app.Shutdown(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) Shutdown(ctx context.Context) error {
|
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)
|
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
app.scrapper.Stop()
|
||||||
|
|
||||||
_ = ctx
|
_ = ctx
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue