-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
55 lines (48 loc) · 1.18 KB
/
status.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
package gospider
import (
"sync/atomic"
"time"
)
// SpiderStatus 爬虫状态
type SpiderStatus struct { // TODO
TotalTask int64 // task总数
FinishedTask int64 // 已完成的任务数
TotalItem int64 // Item的总数
ExecSpeed int64 // 执行数据
itemSpeed int64
}
// NewSpiderStatus 爬虫状态初始化函数
func NewSpiderStatus() *SpiderStatus {
s := &SpiderStatus{}
lastFinish := int64(0)
lastItem := int64(0)
go func() {
for true {
s.ExecSpeed = (s.FinishedTask - lastFinish) / 5
s.itemSpeed = (s.TotalItem - lastItem) / 5
lastFinish = s.FinishedTask
lastItem = s.TotalItem
time.Sleep(5 * time.Second)
}
}()
return s
}
// AddTask 增加task, 并记录在内存中
func (s *SpiderStatus) AddTask() {
atomic.AddInt64(&s.TotalTask, 1)
}
// AddItem 新增 Item
func (s *SpiderStatus) AddItem() {
atomic.AddInt64(&s.TotalTask, 1)
}
// FinishTask 新增完成任务
func (s *SpiderStatus) FinishTask() {
atomic.AddInt64(&s.FinishedTask, 1)
}
// PrintSignalLine 打印爬虫
func (s *SpiderStatus) PrintSignalLine(name string) {
log.Info().
Str("spider", name).
Int64("items/sec", s.TotalItem).
Int64("task finished/sec", s.itemSpeed).Send()
}