-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrawling.go
235 lines (190 loc) · 5.23 KB
/
crawling.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package crawling
import (
"context"
"encoding/json"
"fmt"
mapset "github.com/deckarep/golang-set"
"github.com/eric2788/PlatformsCrawler/file"
"github.com/eric2788/PlatformsCrawler/logging"
"github.com/eric2788/common-utils/array"
"github.com/go-redis/redis/v8"
"strings"
"sync"
"time"
)
var (
logger = logging.GetMainLogger()
ctx = context.Background()
exceptionTopics = mapset.NewSet()
)
// StartCrawling remember use via go
func StartCrawling(tick *time.Ticker, ctx context.Context, stop chan<- struct{}) {
InitRedis()
// 啟用列表,以此爬蟲列表為準
var enabledCrawlers = make(map[string]*Crawling)
logger.Debugf("disabled crawlers: %v", file.ApplicationYaml.DisabledCrawlers)
for tag, crawling := range crawlers {
disabled := array.IndexOfString(file.ApplicationYaml.DisabledCrawlers, tag) != -1
logger.Debugf("[%s] %s 是否禁用: %v", tag, crawling.Name, disabled)
// 在禁用列表內
if disabled {
logger.Infof("爬蟲 %s 已被禁用", tag)
continue
}
enabledCrawlers[tag] = crawling
}
defer stopAllAndWait(stop, enabledCrawlers)
// 啟動所有爬蟲
for _, crawling := range enabledCrawlers {
crawling.Crawler.Start()
}
for {
select {
case <-ctx.Done():
return
case _, ok := <-tick.C:
if !ok {
return
}
for _, crawling := range enabledCrawlers {
publisher := getPublisherFunc(cli, crawling)
go crawlEach(cli, crawling, publisher)
}
}
}
}
func getPublisherFunc(client *redis.Client, crawling *Crawling) Publisher {
return func(room string, arg interface{}) {
prefix := crawling.Crawler.Prefix()
topic := fmt.Sprintf("%s:%s", prefix, room)
if _, ok := arg.([]byte); !ok {
obj, err := json.Marshal(arg)
if err == nil {
arg = obj
} else {
logger.Warnf("嘗試轉換 %+v 為 JSON內容 時出現錯誤: %v", arg, err)
}
}
if err := client.Publish(ctx, topic, arg).Err(); err != nil {
logger.Errorf("嘗試推送訊息到 topic %s 時出現錯誤: %v", topic, err)
logger.Errorf("推送訊息: %+v", arg)
// does it need cancel the pubsub when error ?
}
}
}
func crawlEach(client *redis.Client, crawling *Crawling, publisher Publisher) {
listeningSet, spec, crawler, cLogger := crawling.Listening, crawling.spec, crawling.Crawler, crawling.Logger
topics, err := client.PubSubChannels(ctx, fmt.Sprintf("%s:*", crawler.Prefix())).Result()
if err != nil {
cLogger.Warnf("嘗試獲取 %s 的 pubsub channels 時出現錯誤: %v", crawler.Prefix(), err)
return
}
topicSet := mapset.NewSet()
for _, channel := range topics {
topic := strings.ReplaceAll(channel, fmt.Sprintf("%s:", crawler.Prefix()), "")
if exceptionTopics.Contains(topic) {
continue
} else if !crawler.IsValidTopic(topic) {
cLogger.Warnf("%s 不是一個有效的 topic, 已略過。", topic)
exceptionTopics.Add(topic)
continue
} else {
topicSet.Add(topic)
}
}
switch cc := crawler.(type) {
// 每次新增監聽
case EachCrawling:
each := spec.(*EachSpec)
listenerMap := each.listening
waitGroup := each.wg
toListen := topicSet.Difference(listeningSet)
stopListen := listeningSet.Difference(topicSet)
// 各自監控
for listen := range toListen.Iter() {
t := listen.(string)
waitGroup.Add(1)
canceller := cc.Listen(t, publisher, waitGroup)
listenerMap[t] = canceller
listeningSet.Add(t)
}
// 各自中止
for stop := range stopListen.Iter() {
t := stop.(string)
if cancel, ok := listenerMap[t]; ok {
cancel()
delete(listenerMap, t)
listeningSet.Remove(t)
} else {
cLogger.Errorf("嘗試停止 topic %s 時 發現 不存在於監聽列表。", t)
}
}
// 一次性新增監聽
case OnceCrawling:
diff := listeningSet.SymmetricDifference(topicSet)
i := 0
for range diff.Iter() {
i += 1
}
if i == 0 {
return
}
once := spec.(*OnceSpec)
// 先前已有啟動
if once.stopAll != nil {
// 先中止所有
once.stopAll()
// 等待先前的停止
<-once.waitStop.Done()
}
// 再訂閱所有
toListen := topicSet
oneTimeListen := make([]string, 0)
for listen := range toListen.Iter() {
t := listen.(string)
oneTimeListen = append(oneTimeListen, t)
}
runner, done := context.WithCancel(ctx)
canceller := cc.ListenAll(oneTimeListen, publisher, done)
crawling.Listening = topicSet
once.stopAll = canceller
once.waitStop = runner
default:
logger.Errorf("爬蟲 %s 沒有可用的監控方式。", crawling.Name)
}
}
func stopCrawler(crawling *Crawling, wg *sync.WaitGroup) {
spec := crawling.spec
switch s := spec.(type) {
case *EachSpec:
for _, cancelFunc := range s.listening {
cancelFunc()
}
s.wg.Wait()
case *OnceSpec:
if s.stopAll != nil {
s.stopAll()
<-s.waitStop.Done()
}
default:
logger.Errorf("%s 沒有可用的關閉方式", crawling.Name)
}
wg.Done()
}
func stopAllAndWait(stop chan<- struct{}, crawlers map[string]*Crawling) {
gp := &sync.WaitGroup{}
// stop all topics for each crawler
gp.Add(len(crawlers))
for _, crawling := range crawlers {
go stopCrawler(crawling, gp)
}
gp.Wait()
// stop all crawlers
gp.Add(len(crawlers))
for _, crawling := range crawlers {
go crawling.Crawler.Stop(gp)
}
gp.Wait()
logger.Infof("所有爬蟲已關閉。")
stop <- struct{}{}
}