/
input.go
151 lines (129 loc) · 2.81 KB
/
input.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
package input
import (
"fmt"
"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"
)
// Input is the interface common to all input
type Input interface {
Run()
Stop()
Wait()
}
// Runner encapsulate the lifecycle of the input
type Runner struct {
config inputConfig
input Input
done chan struct{}
wg *sync.WaitGroup
ID uint64
Once bool
beatDone chan struct{}
}
// New instantiates a new Runner
func New(
conf *common.Config,
outlet channel.Factory,
beatDone chan struct{},
states []file.State,
dynFields *common.MapStrPointer,
) (*Runner, error) {
input := &Runner{
config: defaultConfig,
wg: &sync.WaitGroup{},
done: make(chan struct{}),
Once: false,
beatDone: beatDone,
}
var err error
if err = conf.Unpack(&input.config); err != nil {
return nil, err
}
var h map[string]interface{}
conf.Unpack(&h)
input.ID, err = hashstructure.Hash(h, nil)
if err != nil {
return nil, err
}
var f Factory
f, err = GetFactory(input.config.Type)
if err != nil {
return input, err
}
context := Context{
States: states,
Done: input.done,
BeatDone: input.beatDone,
DynamicFields: dynFields,
Meta: nil,
}
var ipt Input
ipt, err = f(conf, outlet, context)
if err != nil {
return input, err
}
input.input = ipt
return input, nil
}
// Start starts the input
func (p *Runner) Start() {
p.wg.Add(1)
logp.Info("Starting input of type: %v; ID: %d ", 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 input is 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 *Runner) Run() {
// Initial input run
p.input.Run()
// Shuts down after the first complete run of all input
if p.Once {
return
}
for {
select {
case <-p.done:
logp.Info("input ticker stopped")
return
case <-time.After(p.config.ScanFrequency):
logp.Debug("input", "Run input")
p.input.Run()
}
}
}
// Stop stops the input and with it all harvesters
func (p *Runner) Stop() {
// Stop scanning and wait for completion
close(p.done)
p.wg.Wait()
}
func (p *Runner) stop() {
logp.Info("Stopping Input: %d", p.ID)
// In case of once, it will be waited until harvesters close itself
if p.Once {
p.input.Wait()
} else {
p.input.Stop()
}
}
func (p *Runner) String() string {
return fmt.Sprintf("input [type=%s, ID=%d]", p.config.Type, p.ID)
}