task-processor/internal/parsers/mandarake/handleTasks.go

59 lines
1.7 KiB
Go
Raw Normal View History

2025-12-26 16:19:09 +03:00
package mandarake
import (
"context"
"github.com/chromedp/chromedp"
log "github.com/sirupsen/logrus"
2026-02-28 10:53:02 +03:00
"runtime"
2025-12-26 16:19:09 +03:00
"sync"
"task-processor/internal/appState"
"task-processor/internal/shared"
)
2026-02-28 10:53:02 +03:00
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)
2025-12-26 16:19:09 +03:00
2026-02-28 10:53:02 +03:00
allocCtx, allocCancel := chromedp.NewRemoteAllocator(ctx, s.externalBrowser)
defer allocCancel()
2025-12-26 16:19:09 +03:00
receiver := make(chan shared.Task, len(tasks))
for _, task := range tasks {
receiver <- task
}
close(receiver)
2026-02-28 10:53:02 +03:00
log.Debugf("%v gorutines before wait group: %v", logHeader, runtime.NumGoroutine())
2025-12-26 16:19:09 +03:00
wg := sync.WaitGroup{}
for i := 0; i < s.goroutinesNumber; i++ {
wg.Add(1)
go func() {
defer wg.Done()
2026-02-28 23:33:22 +03:00
s.worker(allocCtx, receiver, sender)
2025-12-26 16:19:09 +03:00
}()
}
wg.Wait()
2026-02-28 10:53:02 +03:00
log.Debugf("%v gorutines after wait group: %v", logHeader, runtime.NumGoroutine())
log.Infof(logHeader + logWorker + "finished handling tasks")
2025-12-26 16:19:09 +03:00
}
2026-02-28 10:53:02 +03:00
func (s *Parser) worker(ctx context.Context, receiver chan shared.Task, sender chan shared.TaskResult) {
2025-12-26 16:19:09 +03:00
for task := range receiver {
2026-02-28 23:33:22 +03:00
taskCtx, taskCancel := chromedp.NewContext(ctx /* chromedp.WithLogf(log.Printf) */, chromedp.WithLogf(func(string, ...any) {}))
timeoutCtx, timeoutCancel := context.WithTimeout(taskCtx, s.taskTimeout)
2026-02-28 10:53:02 +03:00
log.WithField("task_uuid", task.MerchUuid).Infof("%v %v processing task", logHeader, logWorker)
2025-12-26 16:19:09 +03:00
2026-01-05 17:51:02 +03:00
//price will be zeroPrice value in case of any error or if price not found
2026-02-28 23:33:22 +03:00
price := s.getMinimalPrice(timeoutCtx, task)
2025-12-26 16:19:09 +03:00
sender <- shared.TaskResult{
MerchUuid: task.MerchUuid,
Origin: task.Origin,
Price: price,
}
2026-02-28 23:33:22 +03:00
timeoutCancel()
taskCancel()
2025-12-26 16:19:09 +03:00
}
}