Repository navigation
Expand file tree
/
Copy pathmonitor.go
More file actions
executable file
·221 lines (211 loc) · 5.66 KB
/
Copy pathmonitor.go
File metadata and controls
executable file
·221 lines (211 loc) · 5.66 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
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
package tool
import (
"errors"
sched "evilboy/scheduler"
"fmt"
"runtime"
"time"
)
// 摘要信息的模板。
var summaryForMonitoring = "Monitor - Collected information[%d]:\n" +
" Goroutine number: %d\n" +
" Scheduler:\n%s" +
" Escaped time: %s\n"
// 已达到最大空闲计数的消息模板。
var msgReachMaxIdleCount = "The scheduler has been idle for a period of time" +
" (about %s)." +
" Now consider what stop it."
// 停止调度器的消息模板。
var msgStopScheduler = "Stop scheduler...%s."
// 日志记录函数的类型。
// 参数level代表日志级别。级别设定:0:普通;1:警告;2:错误。
type Record func(level byte, content string)
// 调度器监控函数。
// 参数scheduler代表作为监控目标的调度器。
// 参数intervalNs代表检查间隔时间,单位:纳秒。
// 参数maxIdleCount代表最大空闲计数。
// 参数autoStop被用来指示该方法是否在调度器空闲一段时间(即持续空闲时间,由intervalNs * maxIdleCount得出)之后自行停止调度器。
// 参数detailSummary被用来表示是否需要详细的摘要信息。
// 参数record代表日志记录函数。
// 当监控结束之后,该方法会会向作为唯一返回值的通道发送一个代表了空闲状态检查次数的数值。
func Monitoring(
scheduler sched.Scheduler,
intervalNs time.Duration,
maxIdleCount uint,
autoStop bool,
detailSummary bool,
record Record) <-chan uint64 {
// 调度器不能不可用!
if scheduler == nil {
panic(errors.New("The scheduler is invalid!"))
}
// 防止过小的参数值对爬取流程的影响
if intervalNs < time.Millisecond {
intervalNs = time.Millisecond
}
if maxIdleCount < 1000 {
maxIdleCount = 1000
}
// 监控停止通知器
stopNotifier := make(chan byte, 1)
// 接收和报告错误
reportError(scheduler, record, stopNotifier)
// 记录摘要信息
recordSummary(scheduler, detailSummary, record, stopNotifier)
// 检查计数通道
checkCountChan := make(chan uint64, 2)
// 检查空闲状态
checkStatus(scheduler,
intervalNs,
maxIdleCount,
autoStop,
checkCountChan,
record,
stopNotifier)
return checkCountChan
}
// 检查状态,并在满足持续空闲时间的条件时采取必要措施。
func checkStatus(
scheduler sched.Scheduler,
intervalNs time.Duration,
maxIdleCount uint,
autoStop bool,
checkCountChan chan<- uint64,
record Record,
stopNotifier chan<- byte) {
var checkCount uint64
go func() {
defer func() {
stopNotifier <- 1
stopNotifier <- 2
checkCountChan <- checkCount
}()
// 等待调度器开启
waitForSchedulerStart(scheduler)
// 准备
var idleCount uint
var firstIdleTime time.Time
for {
// 检查调度器的空闲状态
if scheduler.Idle() {
idleCount++
if idleCount == 1 {
firstIdleTime = time.Now()
}
if idleCount >= maxIdleCount {
msg :=
fmt.Sprintf(msgReachMaxIdleCount, time.Since(firstIdleTime).String())
record(0, msg)
// 再次检查调度器的空闲状态,确保它已经可以被停止
if scheduler.Idle() {
if autoStop {
var result string
if scheduler.Stop() {
result = "success"
} else {
result = "failing"
}
msg = fmt.Sprintf(msgStopScheduler, result)
record(0, msg)
}
break
} else {
if idleCount > 0 {
idleCount = 0
}
}
}
} else {
if idleCount > 0 {
idleCount = 0
}
}
checkCount++
time.Sleep(intervalNs)
}
}()
}
// 记录摘要信息。
func recordSummary(
scheduler sched.Scheduler,
detailSummary bool,
record Record,
stopNotifier <-chan byte) {
go func() {
// 等待调度器开启
waitForSchedulerStart(scheduler)
// 准备
var prevSchedSummary sched.SchedSummary
var prevNumGoroutine int
var recordCount uint64 = 1
startTime := time.Now()
for {
// 查看监控停止通知器
select {
case <-stopNotifier:
return
default:
}
// 获取摘要信息的各组成部分
currNumGoroutine := runtime.NumGoroutine()
currSchedSummary := scheduler.Summary(" ")
// 比对前后两份摘要信息的一致性。只有不一致时才会予以记录。
if currNumGoroutine != prevNumGoroutine ||
!currSchedSummary.Same(prevSchedSummary) {
schedSummaryStr := func() string {
if detailSummary {
return currSchedSummary.Detail()
} else {
return currSchedSummary.String()
}
}()
// 记录摘要信息
info := fmt.Sprintf(summaryForMonitoring,
recordCount,
currNumGoroutine,
schedSummaryStr,
time.Since(startTime).String(),
)
record(0, info)
prevNumGoroutine = currNumGoroutine
prevSchedSummary = currSchedSummary
recordCount++
}
time.Sleep(time.Microsecond)
}
}()
}
// 接收和报告错误。
func reportError(
scheduler sched.Scheduler,
record Record,
stopNotifier <-chan byte) {
go func() {
// 等待调度器开启
waitForSchedulerStart(scheduler)
for {
// 查看监控停止通知器
select {
case <-stopNotifier:
return
default:
}
errorChan := scheduler.ErrorChan()
if errorChan == nil {
return
}
err := <-errorChan
if err != nil {
errMsg := fmt.Sprintf("Error (received from error channel): %s", err)
record(2, errMsg)
}
time.Sleep(time.Microsecond)
}
}()
}
// 等待调度器开启。
func waitForSchedulerStart(scheduler sched.Scheduler) {
for !scheduler.Running() {
time.Sleep(time.Microsecond)
}
}