52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package scrapper
|
|
|
|
import (
|
|
"context"
|
|
"github.com/chromedp/chromedp"
|
|
log "github.com/sirupsen/logrus"
|
|
"runtime/debug"
|
|
"scrapper-mandarake/internal/common"
|
|
)
|
|
|
|
func (s *Scrapper) worker(ctx context.Context, tasksChan <-chan common.Task, resultsChan chan<- common.Result) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
log.Errorf("%v %v PANIC: %v\n%s", pkgLogHeader, logWorker, r, debug.Stack())
|
|
}
|
|
}()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case task, ok := <-tasksChan:
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
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", pkgLogHeader, logWorker)
|
|
|
|
//price will be zeroPrice value in case of any error or if price not found
|
|
price := s.getMinimalPrice(timeoutCtx, task)
|
|
result := common.Result{
|
|
MerchUuid: task.MerchUuid,
|
|
OriginName: originName,
|
|
Price: price,
|
|
}
|
|
|
|
select {
|
|
case resultsChan <- result:
|
|
case <-ctx.Done():
|
|
timeoutCancel()
|
|
taskCancel()
|
|
return
|
|
}
|
|
|
|
timeoutCancel()
|
|
taskCancel()
|
|
}
|
|
}
|
|
}
|