forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprospector.go
148 lines (126 loc) · 2.95 KB
/
prospector.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
package prospector
import (
"sync"
"time"
"github.com/mitchellh/hashstructure"
"github.com/elastic/beats/filebeat/channel"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
// Prospectorer is the interface common to all prospectors
type Prospectorer interface {
Run()
Stop()
Wait()
}
// Prospector contains the prospector
type Prospector struct {
config prospectorConfig
prospectorer Prospectorer
done chan struct{}
wg *sync.WaitGroup
id uint64
Once bool
beatDone chan struct{}
}
// NewProspector instantiates a new prospector
func New(
conf *common.Config,
outlet channel.Factory,
beatDone chan struct{},
states []file.State,
) (*Prospector, error) {
prospector := &Prospector{
config: defaultConfig,
wg: &sync.WaitGroup{},
done: make(chan struct{}),
Once: false,
beatDone: beatDone,
}
var err error
if err = conf.Unpack(&prospector.config); err != nil {
return nil, err
}
var h map[string]interface{}
conf.Unpack(&h)
prospector.id, err = hashstructure.Hash(h, nil)
if err != nil {
return nil, err
}
var f Factory
f, err = GetFactory(prospector.config.Type)
if err != nil {
return prospector, err
}
context := Context{
States: states,
Done: prospector.done,
BeatDone: prospector.beatDone,
}
var prospectorer Prospectorer
prospectorer, err = f(conf, outlet, context)
if err != nil {
return prospector, err
}
prospector.prospectorer = prospectorer
return prospector, nil
}
// Start starts the prospector
func (p *Prospector) Start() {
p.wg.Add(1)
logp.Info("Starting prospector of type: %v; id: %v ", p.config.Type, p.ID())
onceWg := sync.WaitGroup{}
if p.Once {
// Make sure start is only completed when Run did a complete first scan
defer onceWg.Wait()
}
onceWg.Add(1)
// Add waitgroup to make sure prospectors finished
go func() {
defer func() {
onceWg.Done()
p.stop()
p.wg.Done()
}()
p.Run()
}()
}
// Run starts scanning through all the file paths and fetch the related files. Start a harvester for each file
func (p *Prospector) Run() {
// Initial prospector run
p.prospectorer.Run()
// Shuts down after the first complete run of all prospectors
if p.Once {
return
}
for {
select {
case <-p.done:
logp.Info("Prospector ticker stopped")
return
case <-time.After(p.config.ScanFrequency):
logp.Debug("prospector", "Run prospector")
p.prospectorer.Run()
}
}
}
// ID returns prospector identifier
func (p *Prospector) ID() uint64 {
return p.id
}
// Stop stops the prospector and with it all harvesters
func (p *Prospector) Stop() {
// Stop scanning and wait for completion
close(p.done)
p.wg.Wait()
}
func (p *Prospector) stop() {
logp.Info("Stopping Prospector: %v", p.ID())
// In case of once, it will be waited until harvesters close itself
if p.Once {
p.prospectorer.Wait()
} else {
p.prospectorer.Stop()
}
}