price scrapper
This commit is contained in:
parent
d19f5f7621
commit
8922b8a4f0
7 changed files with 426 additions and 0 deletions
52
internal/scrapper/worker.go
Normal file
52
internal/scrapper/worker.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue