forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
66 lines (56 loc) · 1.44 KB
/
runner.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
package module
import (
"sync"
"github.com/elastic/beats/libbeat/publisher"
)
// Runner is a facade for a Wrapper that provides a simple interface
// for starting and stopping a Module.
type Runner interface {
// Start starts the Module. If Start is called more than once, only the
// first will start the Module.
Start()
// Stop stops the Module and waits for module's MetricSets to exit. The
// publisher.Client will be closed by Stop. If Stop is called more than
// once, only the first stop the Module and wait for it to exit.
Stop()
// Added to be consistent with cfgfile.Runner
ID() uint64
}
// NewRunner returns a Runner facade. The events generated by
// the Module will be published to a new publisher.Client generated from the
// pubClientFactory.
func NewRunner(pubClientFactory func() publisher.Client, mod *Wrapper) Runner {
return &runner{
done: make(chan struct{}),
mod: mod,
client: pubClientFactory(),
}
}
type runner struct {
done chan struct{}
wg sync.WaitGroup
startOnce sync.Once
stopOnce sync.Once
mod *Wrapper
client publisher.Client
}
func (mr *runner) Start() {
mr.startOnce.Do(func() {
output := mr.mod.Start(mr.done)
mr.wg.Add(1)
go func() {
defer mr.wg.Done()
PublishChannels(mr.client, output)
}()
})
}
func (mr *runner) Stop() {
mr.stopOnce.Do(func() {
close(mr.done)
mr.client.Close()
mr.wg.Wait()
})
}
func (mr *runner) ID() uint64 {
return mr.mod.Hash()
}