-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (168 loc) · 4.43 KB
/
main.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
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
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"runtime/pprof"
"sync"
"syscall"
"time"
"github.com/gocolly/redisstorage"
"github.com/kelseyhightower/envconfig"
"github.com/samirettali/serviceregistry"
"github.com/samirettali/tor-spider/spider"
log "github.com/sirupsen/logrus"
)
type config struct {
RedisURI string `envconfig:"REDIS_URI" required:"true" split_words:"true"`
BlacklistFile string `envconfig:"BLACKLIST_FILE" required:"false" split_words:"true"`
LogLevel string `envconfig:"LOG_LEVEL" default:"error" split_words:"true"`
LogStatusInterval int `envconfig:"LOG_INTERVAL" default:"3" split_words:"true"`
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func main() {
f, err := os.Create("./data/tor-spider.pprof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
logger := log.New()
logger.SetReportCaller(true)
logger.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
var cfg config
if err := envconfig.Process("", &cfg); err != nil {
logger.Fatal(err)
}
logger.Infof("%+v", cfg)
switch {
case cfg.LogLevel == "error":
logger.SetLevel(log.ErrorLevel)
case cfg.LogLevel == "info":
logger.SetLevel(log.InfoLevel)
case cfg.LogLevel == "debug":
logger.SetLevel(log.DebugLevel)
default:
logger.Fatalf("Invalid debug level: %s", cfg.LogLevel)
}
visitedStorage := &redisstorage.Storage{
Address: cfg.RedisURI,
Password: "",
DB: 0,
Prefix: "0",
}
// defer func() {
// visitedStorage.Client.Close()
// }()
registry := serviceregistry.NewServiceRegistry(logger)
// ElasticPageStorage configuration
var elasticConfig spider.ElasticPageConfig
err = envconfig.Process("ELASTIC_PAGE_STORAGE", &elasticConfig)
if err != nil {
logger.Fatal(err)
}
log.Infof("%+v\n", elasticConfig)
elasticPageStorage, err := spider.NewElasticPageStorage(&elasticConfig)
if err != nil {
logger.Fatal(err)
}
elasticPageStorage.Logger = logger
if err := registry.RegisterService(elasticPageStorage); err != nil {
logger.Fatal(err)
}
// MongoJobsStorage configuration
var mongoConfig spider.MongoJobsConfig
err = envconfig.Process("MONGO_JOBS_STORAGE", &mongoConfig)
if err != nil {
logger.Fatal(err)
}
log.Infof("%+v\n", mongoConfig)
mongoJobsStorage, err := spider.NewMongoJobsStorage(&mongoConfig)
if err != nil {
logger.Fatal(err)
}
mongoJobsStorage.Logger = logger
if err := registry.RegisterService(mongoJobsStorage); err != nil {
logger.Fatal(err)
}
// Spider configuration
// TODO use generic interface as it's meant to be done. For now this will
// do.
var jobsStorage *spider.MongoJobsStorage
if err := registry.FetchService(&jobsStorage); err != nil {
logger.Fatal(err)
}
var pageStorage *spider.ElasticPageStorage
if err := registry.FetchService(&pageStorage); err != nil {
logger.Fatal(err)
}
var spiderConfig spider.SpiderConfig
if err := envconfig.Process("SPIDER", &spiderConfig); err != nil {
logger.Fatal(err)
}
log.Infof("%+v", spiderConfig)
spiderConfig.JobsStorage = jobsStorage
spiderConfig.PageStorage = pageStorage
spiderConfig.VisitedStorage = visitedStorage
if cfg.BlacklistFile != "" {
blacklist, err := readLines(cfg.BlacklistFile)
if err != nil {
log.Fatal("Error while reading " + cfg.BlacklistFile)
}
spiderConfig.Blacklist = blacklist
}
spider, err := spider.NewSpider(&spiderConfig)
if err != nil {
logger.Fatal(err)
}
spider.Logger = logger
if err := registry.RegisterService(spider); err != nil {
logger.Fatal(err)
}
registry.StartAll()
stop := make(chan os.Signal, 1)
done := make(chan struct{})
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
ticker := time.NewTicker(time.Second * time.Duration(cfg.LogStatusInterval))
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-stop
registry.StopAll()
close(done)
wg.Done()
}()
for {
select {
case <-ticker.C:
statuses := registry.Statuses()
for kind, status := range statuses {
logger.Infof("%s %s", kind, status)
}
fmt.Println()
case <-done:
statuses := registry.Statuses()
fmt.Println("All services stopped")
for kind, status := range statuses {
logger.Infof("%s %s", kind, status)
}
wg.Wait()
return
}
}
}