-
Notifications
You must be signed in to change notification settings - Fork 487
/
file.go
125 lines (107 loc) · 2.89 KB
/
file.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
package file_match
import (
"context"
"sync"
"time"
"github.com/grafana/agent/component/discovery"
"github.com/go-kit/log/level"
"github.com/grafana/agent/component"
)
func init() {
component.Register(component.Registration{
Name: "local.file_match",
Args: Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
},
})
}
// Arguments holds values which are used to configure the local.file_match
// component.
type Arguments struct {
PathTargets []discovery.Target `river:"path_targets,attr"`
SyncPeriod time.Duration `river:"sync_period,attr,optional"`
}
var _ component.Component = (*Component)(nil)
// Component implements the local.file_match component.
type Component struct {
opts component.Options
mut sync.RWMutex
args Arguments
watches []watch
watchDog *time.Ticker
}
// New creates a new local.file_match component.
func New(o component.Options, args Arguments) (*Component, error) {
c := &Component{
opts: o,
mut: sync.RWMutex{},
args: args,
watches: make([]watch, 0),
watchDog: time.NewTicker(args.SyncPeriod),
}
if err := c.Update(args); err != nil {
return nil, err
}
return c, nil
}
func getDefault() Arguments {
return Arguments{SyncPeriod: 10 * time.Second}
}
// SetToDefault implements river.Defaulter.
func (a *Arguments) SetToDefault() {
*a = getDefault()
}
// Update satisfies the component interface.
func (c *Component) Update(args component.Arguments) error {
c.mut.Lock()
defer c.mut.Unlock()
// Check to see if our ticker timer needs to be reset.
if args.(Arguments).SyncPeriod != c.args.SyncPeriod {
c.watchDog.Reset(c.args.SyncPeriod)
}
c.args = args.(Arguments)
c.watches = c.watches[:0]
for _, v := range c.args.PathTargets {
c.watches = append(c.watches, watch{
target: v,
log: c.opts.Logger,
})
}
return nil
}
// Run satisfies the component interface.
func (c *Component) Run(ctx context.Context) error {
update := func() {
c.mut.Lock()
defer c.mut.Unlock()
paths := c.getWatchedFiles()
// The component node checks to see if exports have actually changed.
c.opts.OnStateChange(discovery.Exports{Targets: paths})
}
// Trigger initial check
update()
defer c.watchDog.Stop()
for {
select {
case <-c.watchDog.C:
// This triggers a check for any new paths, along with pushing new targets.
update()
case <-ctx.Done():
return nil
}
}
}
func (c *Component) getWatchedFiles() []discovery.Target {
paths := make([]discovery.Target, 0)
// See if there is anything new we need to check.
for _, w := range c.watches {
newPaths, err := w.getPaths()
if err != nil {
level.Error(c.opts.Logger).Log("msg", "error getting paths", "path", w.getPath(), "excluded", w.getExcludePath(), "err", err)
}
paths = append(paths, newPaths...)
}
return paths
}