-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.go
More file actions
50 lines (45 loc) · 1.12 KB
/
plugin.go
File metadata and controls
50 lines (45 loc) · 1.12 KB
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
package youcrawl
import (
"fmt"
"github.com/sirupsen/logrus"
"time"
)
type Plugin interface {
Run(e *Engine)
}
const (
// total
STATUS_KEY_TOTAL = "status.total"
// unrequested count
STATUS_KEY_UNREQUESTED = "status.unrequested"
// complete count
STATUS_KEY_COMPLETE = "status.complete"
// speed
STATUS_KEY_SPEED = "status.speed"
)
// log engine status plugin
type StatusOutputPlugin struct {
// disable log output
LogOutput bool
}
func (p *StatusOutputPlugin) Run(e *Engine) {
lastComplete := 0
for {
total, _ := e.Pool.GetTotal()
unrequested, _ := e.Pool.GetUnRequestCount()
complete, _ := e.Pool.GetCompleteCount()
speed := complete - lastComplete
if p.LogOutput {
logrus.WithField("scope","status-report").Info(fmt.Sprintf(
"total: %d,unrequest: %d,complete: %d,speed: %d/s",
total,unrequested,complete,speed,
))
}
e.GlobalStore.SetValue(STATUS_KEY_TOTAL,total)
e.GlobalStore.SetValue(STATUS_KEY_UNREQUESTED,unrequested)
e.GlobalStore.SetValue(STATUS_KEY_COMPLETE,complete)
e.GlobalStore.SetValue(STATUS_KEY_SPEED,speed)
lastComplete = complete
<-time.After(1 * time.Second)
}
}