-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
65 lines (55 loc) · 1.09 KB
/
service.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
package proxy_srv
import (
"fmt"
"proxy_srv/pool_services/colly_kuaidaili_proxy"
"proxy_srv/proxy_host"
"proxy_srv/router"
"runtime/debug"
"time"
)
type ProxySrv struct {
// 这里放各个站点的开关, 默认开启
IgnoreKuaidaili bool
// 这里是爬取各站点的频率
CrawlFrequency time.Duration
// 这里是检查失效的代理的频率
ClearHostFrequency time.Duration
Debug bool
RunWeb bool
}
type cronFunc func()
func start(f cronFunc) {
defer func() {
if r := recover(); r != nil {
fmt.Println("任务异常 : ", r)
fmt.Println(string(debug.Stack()))
}
}()
f()
}
func cron(d time.Duration, f cronFunc) {
for {
start(f)
time.Sleep(d)
}
}
func (s *ProxySrv) crawl() {
if !s.IgnoreKuaidaili {
kuaidailiSrv := colly_kuaidaili_proxy.CollySrv{
UseProxy: true,
}
kuaidailiSrv.Run()
}
}
func (s *ProxySrv) Run() {
go cron(s.CrawlFrequency, s.crawl)
proxy_host.Debug = s.Debug
go cron(s.ClearHostFrequency, proxy_host.ClearBadProxyHosts)
if s.RunWeb {
web := router.Init()
err := web.Run(":8081")
if err != nil {
panic(err)
}
}
}