-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
102 lines (87 loc) · 2.08 KB
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package xivish
import (
"fmt"
"time"
"github.com/gocolly/colly/v2/queue"
)
// SetupQueue sets up the queue for the scraper
func (s *Scraper) AttachQueue(threads int, maxSize int) error {
q, err := queue.New(
threads,
&queue.InMemoryQueueStorage{MaxSize: maxSize},
)
if err != nil {
s.logger.LogError("queue", err)
return err
}
s.queue = q
s.logger.LogInfo("queue created", fmt.Sprintf("%d threads and max size of %d", threads, maxSize))
return nil
}
// addUrlToQueue adds an url to the queue
func (s Scraper) addUrlToQueue(url string) error {
if s.queue == nil {
s.logger.LogError("scraper", "no queue available")
return fmt.Errorf("no queue available")
}
err := s.queue.AddURL(url)
if err != nil {
s.logger.LogError("queue", err)
return err
}
return nil
}
// StartQueue initiates scraping with the queue
func (s Scraper) StartQueue() error {
if s.collector == nil {
s.logger.LogError("scraper", "no collector available")
return fmt.Errorf("no collector available")
}
if s.queue == nil {
s.logger.LogError("scraper", "no queue available")
return fmt.Errorf("no queue available")
}
size, err := s.queue.Size()
if err != nil {
s.logger.LogError("queue size", err)
return err
}
s.logger.LogInfo("queue status", fmt.Sprintf("started with %d items", size))
s.startQueueLogger()
err = s.queue.Run(s.collector)
if err != nil {
s.logger.LogError("queue", err)
return err
}
s.logger.LogInfo("queue status", "finished")
return nil
}
// startQueueLogger starts the queue logger in the background
func (s *Scraper) startQueueLogger() {
if (s.logger.Queue == LoggerQueueCfg{}) || !s.logger.Queue.Enabled {
return
}
s.logger.Queue.stop = make(chan bool)
go func() {
loop:
for {
select {
case <-s.logger.Queue.stop:
break loop
default:
time.Sleep(s.logger.Queue.Interval)
s.logQueueProgress()
}
}
}()
}
// logQueueProgress logs the queue progress
func (s Scraper) logQueueProgress() {
size, err := s.queue.Size()
if err != nil {
s.logger.LogError("queue size", err)
}
if size > 0 {
s.logger.LogInfo("queue size", size)
}
}