-
Notifications
You must be signed in to change notification settings - Fork 10
/
scrapemate.go
626 lines (481 loc) · 12.5 KB
/
scrapemate.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package scrapemate
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"
"github.com/gosom/kit/logging"
)
// New creates a new scrapemate
func New(options ...func(*ScrapeMate) error) (*ScrapeMate, error) {
s := &ScrapeMate{}
for _, opt := range options {
if err := opt(s); err != nil {
return nil, err
}
}
if s.jobProvider == nil {
return nil, ErrorNoJobProvider
}
if s.httpFetcher == nil {
return nil, ErrorNoHTMLFetcher
}
// here we can set default options
s.results = make(chan Result)
if s.ctx == nil {
s.ctx, s.cancelFn = context.WithCancelCause(context.Background())
}
if s.cancelFn == nil {
s.ctx, s.cancelFn = context.WithCancelCause(s.ctx)
}
if s.log == nil {
s.log = logging.Get().With("component", "scrapemate")
s.log.Debug("using default logger")
}
if s.concurrency == 0 {
s.concurrency = 1
}
return s, nil
}
func WithExitBecauseOfInactivity(duration time.Duration) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
s.exitOnInactivity = duration > 0
s.exitOnInactivityDuration = duration
return nil
}
}
// WithFailed sets the failed jobs channel for the scrapemate
func WithFailed() func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
s.failedJobs = make(chan IJob)
return nil
}
}
// WithContext sets the context for the scrapemate
func WithContext(ctx context.Context, cancelFn context.CancelCauseFunc) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if ctx == nil {
return ErrorNoContext
}
s.ctx = ctx
s.cancelFn = cancelFn
return nil
}
}
// WithLogger sets the logger for the scrapemate
func WithLogger(log logging.Logger) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if log == nil {
return ErrorNoLogger
}
s.log = log
return nil
}
}
// WithJobProvider sets the job provider for the scrapemate
func WithJobProvider(provider JobProvider) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if provider == nil {
return errors.New("job provider is nil")
}
s.jobProvider = provider
return nil
}
}
// WithConcurrency sets the concurrency for the scrapemate
func WithConcurrency(concurrency int) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if concurrency < 1 {
return ErrorConcurrency
}
s.concurrency = concurrency
return nil
}
}
// WithHTTPFetcher sets the http fetcher for the scrapemate
func WithHTTPFetcher(client HTTPFetcher) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if client == nil {
return ErrorNoHTMLFetcher
}
s.httpFetcher = client
return nil
}
}
// WithHTMLParser sets the html parser for the scrapemate
func WithHTMLParser(parser HTMLParser) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if parser == nil {
return ErrorNoHTMLParser
}
s.htmlParser = parser
return nil
}
}
// WithCache sets the cache for the scrapemate
func WithCache(cache Cacher) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
if cache == nil {
return ErrorNoCacher
}
s.cache = cache
return nil
}
}
// WithInitJob sets the first job to be processed
// It will be processed before the jobs from the job provider
// It is useful if you want to start the scraper with a specific job
// instead of the first one from the job provider
// A real use case is when you want to obtain some cookies before starting
// the scraping process (e.g. login)
// Important: The results from these job will be discarded !
func WithInitJob(job IJob) func(*ScrapeMate) error {
return func(s *ScrapeMate) error {
s.initJob = job
return nil
}
}
// Scrapemate contains unexporter fields
type ScrapeMate struct {
log logging.Logger
ctx context.Context
cancelFn context.CancelCauseFunc
jobProvider JobProvider
concurrency int
httpFetcher HTTPFetcher
htmlParser HTMLParser
cache Cacher
results chan Result
failedJobs chan IJob
initJob IJob
stats stats
exitOnInactivity bool
exitOnInactivityDuration time.Duration
}
// Start starts the scraper
func (s *ScrapeMate) Start() error {
s.log.Info("starting scrapemate")
defer func() {
close(s.results)
if s.failedJobs != nil {
close(s.failedJobs)
}
s.log.Info("scrapemate exited")
}()
exitChan := make(chan os.Signal, 1)
signal.Notify(exitChan, os.Interrupt, syscall.SIGTERM)
s.waitForSignal(exitChan)
if err := s.processInitJob(s.ctx); err != nil {
return err
}
wg := sync.WaitGroup{}
wg.Add(s.concurrency)
for i := 0; i < s.concurrency; i++ {
go func() {
defer wg.Done()
s.startWorker(s.ctx)
}()
}
wg.Add(1)
go func() {
defer wg.Done()
startTime := time.Now().UTC()
tickerDur := time.Minute
const (
divider = 2
secondsPerMinute = 60
)
if s.exitOnInactivity && s.exitOnInactivityDuration < tickerDur {
tickerDur = s.exitOnInactivityDuration / divider
}
ticker := time.NewTicker(tickerDur)
defer ticker.Stop()
for {
select {
case <-s.ctx.Done():
return
case <-ticker.C:
numOfJobsCompleted, numOfJobsFailed, lastActivityAt := s.stats.getStats()
perMinute := float64(numOfJobsCompleted) / time.Now().UTC().Sub(startTime).Seconds() * secondsPerMinute
s.log.Info("scrapemate stats",
"numOfJobsCompleted", numOfJobsCompleted,
"numOfJobsFailed", numOfJobsFailed,
"lastActivityAt", lastActivityAt,
"speed", fmt.Sprintf("%.2f jobs/min", perMinute),
)
if s.exitOnInactivity && time.Now().UTC().Sub(lastActivityAt) > s.exitOnInactivityDuration {
err := fmt.Errorf("%w: %s", ErrInactivityTimeout, lastActivityAt.Format(time.RFC3339))
s.log.Info("exiting because of inactivity", "error", err)
s.cancelFn(err)
return
}
}
}
}()
wg.Wait()
<-s.Done()
return s.Err()
}
func (s *ScrapeMate) Close() error {
_ = s.httpFetcher.Close()
return nil
}
// Concurrency returns how many workers are running in parallel
func (s *ScrapeMate) Concurrency() int {
return s.concurrency
}
// Results returns a channel containing the results
func (s *ScrapeMate) Results() <-chan Result {
return s.results
}
// Failed returns the chanell that contains the jobs that failed. It's nil if
// you don't use the WithFailed option
func (s *ScrapeMate) Failed() <-chan IJob {
return s.failedJobs
}
// DoJob scrapes a job and returns it's result
func (s *ScrapeMate) DoJob(ctx context.Context, job IJob) (result any, next []IJob, err error) {
ctx = ContextWithLogger(ctx, s.log.With("jobid", job.GetID()))
startTime := time.Now().UTC()
s.log.Debug("starting job", "job", job)
var resp Response
defer func() {
args := []any{
"job", job,
}
if r := recover(); r != nil {
args = append(args, "error", r, "status", "failed")
stack := string(debug.Stack())
err = fmt.Errorf("panic while executing job: %s", stack)
args = append(args, "error", err)
s.log.Error("job finished", args...)
return
}
if resp.Error != nil {
args = append(args, "error", resp.Error, "status", "failed")
} else {
args = append(args, "status", "success")
}
args = append(args, "duration", time.Now().UTC().Sub(startTime))
s.log.Info("job finished", args...)
}()
var cached bool
cacheKey := job.GetCacheKey()
if s.cache != nil {
var errCache error
resp, errCache = s.cache.Get(ctx, cacheKey)
if errCache == nil {
cached = true
}
}
switch {
case cached:
s.log.Debug("using cached response", "job", job)
default:
resp = s.doFetch(ctx, job)
if !job.ProcessOnFetchError() && resp.Error != nil {
err = resp.Error
return nil, nil, err
}
// check if resp.Error is valid because we may ProcessOnFetchError
if resp.Error == nil && s.cache != nil {
if errCache := s.cache.Set(ctx, cacheKey, &resp); errCache != nil {
s.log.Error("error while caching response", "error", errCache, "job", job)
}
}
}
// process the response if we have a html parser and the resp has no error
if resp.Error == nil && s.htmlParser != nil {
resp.Document, err = s.htmlParser.Parse(ctx, resp.Body)
if err != nil {
s.log.Error("error while setting document", "error", err)
return nil, nil, err
}
}
result, next, err = job.Process(ctx, &resp)
if err != nil {
// TODO shall I retry?
s.log.Error("error while processing job", "error", err)
return nil, nil, err
}
return result, next, nil
}
func (s *ScrapeMate) doFetch(ctx context.Context, job IJob) (ans Response) {
var ok bool
defer func() {
if !ok && ans.Error == nil {
ans.Error = fmt.Errorf("status code %d", ans.StatusCode)
}
}()
maxRetries := s.getMaxRetries(job)
const defaultMilliseconds = 100
delay := time.Millisecond * defaultMilliseconds
retryPolicy := job.GetRetryPolicy()
retry := 0
for {
ans = s.httpFetcher.Fetch(ctx, job)
ok = job.DoCheckResponse(&ans)
if ok {
return ans
}
if retryPolicy == DiscardJob {
s.log.Warn("discarding job because of policy")
return ans
}
if retryPolicy == StopScraping {
s.log.Warn("stopping scraping because of policy")
s.cancelFn(errors.New("stopping scraping because of policy"))
return ans
}
if retry >= maxRetries {
return ans
}
retry++
switch retryPolicy {
case RetryJob:
time.Sleep(delay)
if delay > job.GetMaxRetryDelay() {
delay = job.GetMaxRetryDelay()
} else {
delay *= 2
}
case RefreshIP: // TODO Implement
}
}
}
func (s *ScrapeMate) getMaxRetries(job IJob) int {
const maxRetriesDefault = 5
maxRetries := job.GetMaxRetries()
if maxRetries > maxRetriesDefault {
maxRetries = maxRetriesDefault
}
return maxRetries
}
// Done returns a channel that's closed when the work is done
func (s *ScrapeMate) Done() <-chan struct{} {
return s.ctx.Done()
}
// Err returns the error that caused scrapemate's context cancellation
func (s *ScrapeMate) Err() error {
err := context.Cause(s.ctx)
if errors.Is(err, ErrInactivityTimeout) {
return nil
}
return err
}
func (s *ScrapeMate) waitForSignal(sigChan <-chan os.Signal) {
go func() {
<-sigChan
s.log.Info("received signal, shutting down")
s.cancelFn(ErrorExitSignal)
}()
}
func (s *ScrapeMate) processInitJob(ctx context.Context) error {
if s.initJob == nil {
return nil
}
s.log.Info("processing init", "job", s.initJob)
defer s.log.Info("init job finished", "job", s.initJob)
var stack []IJob
if s.initJob != nil {
stack = append(stack, s.initJob)
}
var job IJob
for len(stack) > 0 {
select {
case <-ctx.Done():
return nil
default:
}
job, stack = stack[0], stack[1:]
_, next, err := s.DoJob(ctx, job)
if err != nil {
return err
}
stack = append(stack, next...)
}
return nil
}
func (s *ScrapeMate) startWorker(ctx context.Context) {
jobc, errc := s.jobProvider.Jobs(ctx)
for {
select {
case <-ctx.Done():
return
case err := <-errc:
if ctx.Err() == context.Canceled {
return
}
s.log.Error("error while getting jobs...going to wait a bit", "error", err)
time.Sleep(1 * time.Second)
jobc, errc = s.jobProvider.Jobs(ctx)
s.log.Info("restarted job provider")
case job := <-jobc:
ans, next, err := s.DoJob(ctx, job)
if err != nil {
s.log.Error("error while processing job", "error", err)
s.pushToFailedJobs(job)
continue
}
if err := s.finishJob(ctx, job, ans, next); err != nil {
s.log.Error("error while finishing job", "error", err)
s.pushToFailedJobs(job)
}
}
}
}
func (s *ScrapeMate) pushToFailedJobs(job IJob) {
s.stats.incJobsFailed()
if s.failedJobs != nil {
s.failedJobs <- job
}
}
func (s *ScrapeMate) finishJob(ctx context.Context, job IJob, ans any, next []IJob) error {
s.stats.incJobsCompleted()
if err := s.pushJobs(ctx, next); err != nil {
return fmt.Errorf("%w: while pushing jobs", err)
}
if job.UseInResults() {
s.results <- Result{
Job: job,
Data: ans,
}
}
return nil
}
func (s *ScrapeMate) pushJobs(ctx context.Context, jobs []IJob) error {
for i := range jobs {
if err := s.jobProvider.Push(ctx, jobs[i]); err != nil {
return err
}
}
return nil
}
type stats struct {
l sync.RWMutex
numOfJobsCompleted int64
numOfJobsFailed int64
lastActivityAt time.Time
}
func (o *stats) getStats() (completed, failed int64, lastActivityAt time.Time) {
o.l.RLock()
defer o.l.RUnlock()
return o.numOfJobsCompleted, o.numOfJobsFailed, o.lastActivityAt
}
func (o *stats) incJobsCompleted() {
o.l.Lock()
defer o.l.Unlock()
o.numOfJobsCompleted++
o.lastActivityAt = time.Now().UTC()
}
func (o *stats) incJobsFailed() {
o.l.Lock()
defer o.l.Unlock()
o.numOfJobsFailed++
o.lastActivityAt = time.Now().UTC()
}