-
Notifications
You must be signed in to change notification settings - Fork 48
/
crawler.go
166 lines (141 loc) · 3.64 KB
/
crawler.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
// Command crawler demonstrates how to run the crawler
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/abursavich/nett"
"github.com/garyburd/redigo/redis"
"github.com/jivesearch/jivesearch/config"
"github.com/jivesearch/jivesearch/log"
"github.com/jivesearch/jivesearch/search/crawler"
"github.com/jivesearch/jivesearch/search/crawler/queue"
"github.com/jivesearch/jivesearch/search/crawler/robots"
"github.com/jivesearch/jivesearch/search/document"
"github.com/olivere/elastic"
"github.com/spf13/viper"
)
func afterFn(executionID int64, requests []elastic.BulkableRequest, resp *elastic.BulkResponse, err error) {
// NOTE: err can be nil even if documents fail to update
if resp != nil {
failed := resp.Failed()
for _, d := range failed {
log.Info.Printf("document failed: %+v\n", d)
log.Info.Printf(" reason: %+v\n", d.Error)
}
}
if err != nil {
panic(err)
}
}
var (
c *crawler.Crawler
duration time.Duration
)
func setup(v *viper.Viper) {
v.SetEnvPrefix("jivesearch")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
config.SetDefaults(v)
if v.GetBool("debug") {
log.Debug.SetOutput(os.Stdout)
}
duration = v.GetDuration("crawler.time")
c = crawler.New(v)
c.HTTPClient = &http.Client{
Transport: &http.Transport{
Dial: (&nett.Dialer{
Resolver: &nett.CacheResolver{TTL: 10 * time.Minute},
IPFilter: nett.DualStack,
}).Dial,
DisableKeepAlives: true,
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// For robots.txt follow the default redirect policy of max 10 redirects
// For all others don't follow redirects
if strings.ToLower(req.URL.Path) == crawler.RobotsPath.Path {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
return nil
}
return http.ErrUseLastResponse
},
Timeout: v.GetDuration("crawler.timeout"),
}
return
}
func main() {
v := viper.New()
setup(v)
// setup Elasticsearch
// Note: for remote URLs I can't seem to get it to work with sniffing on
// see https://github.com/olivere/elastic/issues/312
ri := v.GetString("elasticsearch.robots.index")
client, err := elastic.NewClient(elastic.SetURL(v.GetString("elasticsearch.url")), elastic.SetSniff(false))
if err != nil {
panic(err)
}
bulk, err := client.BulkProcessor().
After(afterFn).
//BulkActions().
Do(context.Background())
if err != nil {
panic(err)
}
defer bulk.Close()
// setup our search index
c.Backend = &crawler.ElasticSearch{
ElasticSearch: &document.ElasticSearch{
Client: client,
Index: v.GetString("elasticsearch.search.index"),
Type: v.GetString("elasticsearch.search.type"),
},
Bulk: bulk,
}
if err := c.Backend.Setup(); err != nil {
panic(err)
}
// Setup our robots.txt cache
c.Robots = &robots.ElasticSearch{
Client: client,
Bulk: bulk,
Index: ri,
Type: v.GetString("elasticsearch.robots.type"),
}
exists, err := c.Robots.IndexExists()
if err != nil {
panic(err)
}
if !exists {
if err := c.Robots.Setup(); err != nil {
panic(err)
}
}
// Setup our queue
rds := &queue.Redis{
RedisPool: &redis.Pool{
MaxIdle: v.GetInt("crawler.workers"),
MaxActive: v.GetInt("crawler.workers"),
IdleTimeout: 10 * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
cl, err := redis.Dial("tcp", fmt.Sprintf("%v:%v", v.GetString("redis.host"), v.GetString("redis.port")))
if err != nil {
return nil, err
}
return cl, err
},
},
}
defer rds.RedisPool.Close()
c.Queue = rds
defer c.Close()
if err := c.Start(duration); err != nil {
log.Info.Fatalf("%+v", err)
}
}