forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.go
85 lines (67 loc) · 2.1 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
package crawler
import (
"fmt"
"sync"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/filebeat/prospector"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
type Crawler struct {
prospectors []*prospector.Prospector
prospectorConfigs []*common.Config
out prospector.Outlet
wg sync.WaitGroup
}
func New(out prospector.Outlet, prospectorConfigs []*common.Config) (*Crawler, error) {
if len(prospectorConfigs) == 0 {
return nil, fmt.Errorf("No prospectors defined. You must have at least one prospector defined in the config file.")
}
return &Crawler{
out: out,
prospectorConfigs: prospectorConfigs,
}, nil
}
func (c *Crawler) Start(states file.States, once bool) error {
logp.Info("Loading Prospectors: %v", len(c.prospectorConfigs))
// Prospect the globs/paths given on the command line and launch harvesters
for _, prospectorConfig := range c.prospectorConfigs {
prospector, err := prospector.NewProspector(prospectorConfig, states, c.out)
if err != nil {
return fmt.Errorf("Error in initing prospector: %s", err)
}
c.prospectors = append(c.prospectors, prospector)
}
logp.Info("Loading Prospectors completed. Number of prospectors: %v", len(c.prospectors))
for i, p := range c.prospectors {
c.wg.Add(1)
go func(id int, prospector *prospector.Prospector) {
defer func() {
c.wg.Done()
logp.Debug("crawler", "Prospector %v stopped", id)
}()
logp.Debug("crawler", "Starting prospector %v", id)
prospector.Run(once)
}(i, p)
}
logp.Info("All prospectors are initialised and running with %d states to persist", states.Count())
return nil
}
func (c *Crawler) Stop() {
logp.Info("Stopping Crawler")
stopProspector := func(p *prospector.Prospector) {
defer c.wg.Done()
p.Stop()
}
logp.Info("Stopping %v prospectors", len(c.prospectors))
for _, p := range c.prospectors {
// Stop prospectors in parallel
c.wg.Add(1)
go stopProspector(p)
}
c.WaitForCompletion()
logp.Info("Crawler stopped")
}
func (c *Crawler) WaitForCompletion() {
c.wg.Wait()
}