forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
137 lines (122 loc) · 2.82 KB
/
processor.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
package template
import (
"fmt"
"sync"
"time"
"github.com/kelseyhightower/confd/log"
)
type Processor interface {
Process()
}
func Process(config Config) error {
ts, err := getTemplateResources(config)
if err != nil {
return err
}
return process(ts)
}
func process(ts []*TemplateResource) error {
var lastErr error
for _, t := range ts {
if err := t.process(); err != nil {
log.Error(err.Error())
lastErr = err
}
}
return lastErr
}
type intervalProcessor struct {
config Config
stopChan chan bool
doneChan chan bool
errChan chan error
interval int
}
func IntervalProcessor(config Config, stopChan, doneChan chan bool, errChan chan error, interval int) Processor {
return &intervalProcessor{config, stopChan, doneChan, errChan, interval}
}
func (p *intervalProcessor) Process() {
defer close(p.doneChan)
for {
ts, err := getTemplateResources(p.config)
if err != nil {
log.Fatal(err.Error())
break
}
process(ts)
select {
case <-p.stopChan:
break
case <-time.After(time.Duration(p.interval) * time.Second):
continue
}
}
}
type watchProcessor struct {
config Config
stopChan chan bool
doneChan chan bool
errChan chan error
wg sync.WaitGroup
}
func WatchProcessor(config Config, stopChan, doneChan chan bool, errChan chan error) Processor {
var wg sync.WaitGroup
return &watchProcessor{config, stopChan, doneChan, errChan, wg}
}
func (p *watchProcessor) Process() {
defer close(p.doneChan)
ts, err := getTemplateResources(p.config)
if err != nil {
log.Fatal(err.Error())
return
}
for _, t := range ts {
t := t
p.wg.Add(1)
go p.monitorPrefix(t)
}
p.wg.Wait()
}
func (p *watchProcessor) monitorPrefix(t *TemplateResource) {
defer p.wg.Done()
keys := appendPrefix(t.Prefix, t.Keys)
for {
index, err := t.storeClient.WatchPrefix(t.Prefix, keys, t.lastIndex, p.stopChan)
if err != nil {
p.errChan <- err
// Prevent backend errors from consuming all resources.
time.Sleep(time.Second * 2)
continue
}
t.lastIndex = index
if err := t.process(); err != nil {
p.errChan <- err
}
}
}
func getTemplateResources(config Config) ([]*TemplateResource, error) {
var lastError error
templates := make([]*TemplateResource, 0)
log.Debug("Loading template resources from confdir " + config.ConfDir)
if !isFileExist(config.ConfDir) {
log.Warning(fmt.Sprintf("Cannot load template resources: confdir '%s' does not exist", config.ConfDir))
return nil, nil
}
paths, err := recursiveFindFiles(config.ConfigDir, "*toml")
if err != nil {
return nil, err
}
if len(paths) < 1 {
log.Warning("Found no templates")
}
for _, p := range paths {
log.Debug(fmt.Sprintf("Found template: %s", p))
t, err := NewTemplateResource(p, config)
if err != nil {
lastError = err
continue
}
templates = append(templates, t)
}
return templates, lastError
}