page timeout added
This commit is contained in:
parent
56309cafb9
commit
f466566505
3 changed files with 17 additions and 9 deletions
|
|
@ -16,9 +16,6 @@ func (s *Parser) HandleTasks(ctx context.Context, tasks []shared.Task, sender ch
|
|||
allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser)
|
||||
defer allocCancel()
|
||||
|
||||
sessionCtx, sessionCancel := chromedp.NewContext(allocCtx /* chromedp.WithLogf(log.Printf) */, chromedp.WithLogf(func(string, ...any) {}))
|
||||
defer sessionCancel()
|
||||
|
||||
receiver := make(chan shared.Task, len(tasks))
|
||||
for _, task := range tasks {
|
||||
receiver <- task
|
||||
|
|
@ -31,7 +28,7 @@ func (s *Parser) HandleTasks(ctx context.Context, tasks []shared.Task, sender ch
|
|||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
s.worker(sessionCtx, receiver, sender)
|
||||
s.worker(allocCtx, receiver, sender)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
|
@ -42,14 +39,20 @@ func (s *Parser) HandleTasks(ctx context.Context, tasks []shared.Task, sender ch
|
|||
|
||||
func (s *Parser) worker(ctx context.Context, receiver chan shared.Task, sender chan shared.TaskResult) {
|
||||
for task := range receiver {
|
||||
taskCtx, taskCancel := chromedp.NewContext(ctx /* chromedp.WithLogf(log.Printf) */, chromedp.WithLogf(func(string, ...any) {}))
|
||||
timeoutCtx, timeoutCancel := context.WithTimeout(taskCtx, s.taskTimeout)
|
||||
|
||||
log.WithField("task_uuid", task.MerchUuid).Infof("%v %v processing task", logHeader, logWorker)
|
||||
|
||||
//price will be zeroPrice value in case of any error or if price not found
|
||||
price := s.getMinimalPrice(ctx, task)
|
||||
price := s.getMinimalPrice(timeoutCtx, task)
|
||||
sender <- shared.TaskResult{
|
||||
MerchUuid: task.MerchUuid,
|
||||
Origin: task.Origin,
|
||||
Price: price,
|
||||
}
|
||||
|
||||
timeoutCancel()
|
||||
taskCancel()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package mandarake
|
|||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -16,22 +17,25 @@ const (
|
|||
type Parser struct {
|
||||
externalBrowser string
|
||||
goroutinesNumber int
|
||||
taskTimeout time.Duration
|
||||
}
|
||||
|
||||
type Deps struct {
|
||||
Enabled bool
|
||||
ExternalBrowser string
|
||||
GoroutinesNumber int
|
||||
TaskTimeout int
|
||||
}
|
||||
|
||||
func NewParser(deps Deps) *Parser {
|
||||
if !deps.Enabled {
|
||||
log.Info(logHeader + "disabled")
|
||||
log.Infof("%v disabled", logHeader)
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Parser{
|
||||
externalBrowser: deps.ExternalBrowser,
|
||||
goroutinesNumber: deps.GoroutinesNumber,
|
||||
taskTimeout: time.Minute * time.Duration(deps.TaskTimeout),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue