context fix
This commit is contained in:
parent
4665c90c2c
commit
c955615fb1
8 changed files with 39 additions and 52 deletions
|
|
@ -4,15 +4,20 @@ import (
|
|||
"context"
|
||||
"github.com/chromedp/chromedp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"runtime"
|
||||
"sync"
|
||||
"task-processor/internal/appState"
|
||||
"task-processor/internal/shared"
|
||||
)
|
||||
|
||||
func (s *Parser) HandleTasks(tasks []shared.Task, sender chan shared.TaskResult, state *appState.State) {
|
||||
log.Debug(logHeader + logWorker + "handling tasks")
|
||||
func (s *Parser) HandleTasks(ctx context.Context, tasks []shared.Task, sender chan shared.TaskResult, state *appState.State) {
|
||||
log.Infof("%v %v handling tasks", logHeader, logWorker)
|
||||
|
||||
allocCtx, allocCancel := chromedp.NewRemoteAllocator(s.baseCtx, s.externalBrowser)
|
||||
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 {
|
||||
|
|
@ -20,41 +25,27 @@ func (s *Parser) HandleTasks(tasks []shared.Task, sender chan shared.TaskResult,
|
|||
}
|
||||
close(receiver)
|
||||
|
||||
log.Debugf("%v gorutines before wait group: %v", logHeader, runtime.NumGoroutine())
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < s.goroutinesNumber; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
s.worker(allocCtx, receiver, sender, state)
|
||||
s.worker(sessionCtx, receiver, sender)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
allocCancel()
|
||||
log.Debug(logHeader + logWorker + "finished handling tasks")
|
||||
|
||||
log.Debugf("%v gorutines after wait group: %v", logHeader, runtime.NumGoroutine())
|
||||
log.Infof(logHeader + logWorker + "finished handling tasks")
|
||||
}
|
||||
|
||||
func (s *Parser) worker(ctx context.Context, receiver chan shared.Task, sender chan shared.TaskResult, state *appState.State) {
|
||||
func (s *Parser) worker(ctx context.Context, receiver chan shared.Task, sender chan shared.TaskResult) {
|
||||
for task := range receiver {
|
||||
log.WithField("task_uuid", task.MerchUuid).Debug(logHeader + logWorker + "processing task")
|
||||
|
||||
//pageCtx, pageCancel := chromedp.NewContext(ctx, chromedp.WithLogf(func(string, ...any) {}))
|
||||
//
|
||||
//price, err := s.getPrice(pageCtx, task)
|
||||
//pageCancel()
|
||||
|
||||
//price, err := s.getMinimalPrice(task)
|
||||
//if err != nil {
|
||||
// log.WithField("task_uuid", task.MerchUuid).Warn(logHeader + logWorker + logTaskWarning + "failed to process, zero price")
|
||||
// sender <- shared.TaskResult{
|
||||
// MerchUuid: task.MerchUuid,
|
||||
// Origin: task.Origin,
|
||||
// Price: zeroPrice,
|
||||
// }
|
||||
// continue
|
||||
//}
|
||||
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(task)
|
||||
price := s.getMinimalPrice(ctx, task)
|
||||
sender <- shared.TaskResult{
|
||||
MerchUuid: task.MerchUuid,
|
||||
Origin: task.Origin,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue