price scrapper
This commit is contained in:
parent
d19f5f7621
commit
8922b8a4f0
7 changed files with 426 additions and 0 deletions
50
internal/scrapper/handleTasks.go
Normal file
50
internal/scrapper/handleTasks.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package scrapper
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/chromedp/chromedp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"scrapper-mandarake/internal/common"
|
||||
)
|
||||
|
||||
func (s *Scrapper) Start(ctx context.Context, tasksChan <-chan common.Task, resultsChan chan<- common.Result) error {
|
||||
if s.goroutinesNumber <= 0 {
|
||||
err := errors.New("gorutines num <= 0, abort")
|
||||
log.WithError(err).Error(pkgLogHeader)
|
||||
return err
|
||||
}
|
||||
|
||||
poolCtx, cancel := context.WithCancel(ctx)
|
||||
s.poolCancel = cancel
|
||||
|
||||
log.Infof("%v Start handling tasks", pkgLogHeader)
|
||||
log.Infof("%v Setting up browser", pkgLogHeader)
|
||||
cr, err := s.setupBrowser(poolCtx)
|
||||
if err != nil {
|
||||
log.WithError(err).Error(pkgLogHeader + logGetPrice + "failed to setup browser")
|
||||
return err
|
||||
}
|
||||
log.WithField("Copyright message", cr).Infof("%v Finished setting up browser.", pkgLogHeader)
|
||||
|
||||
allocCtx, allocCancel := chromedp.NewRemoteAllocator(poolCtx, s.externalBrowser)
|
||||
s.allocCancel = allocCancel
|
||||
|
||||
log.Infof("%v processing tasks...", pkgLogHeader)
|
||||
s.wg.Add(s.goroutinesNumber)
|
||||
for i := 0; i < s.goroutinesNumber; i++ {
|
||||
go s.worker(allocCtx, tasksChan, resultsChan)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scrapper) Stop() {
|
||||
if s.allocCancel != nil {
|
||||
s.allocCancel()
|
||||
}
|
||||
|
||||
if s.poolCancel != nil {
|
||||
s.poolCancel()
|
||||
}
|
||||
s.wg.Wait()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue