This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
156 lines (143 loc) · 3.24 KB
/
config.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
152
153
154
155
156
package main
import (
"bytes"
"io/ioutil"
"os"
"sync"
"time"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
var defaultTimeout = time.Minute
type clientSettings struct {
Gzip bool `yaml:",omitempty"`
Port int
}
type clientConfig struct {
Settings clientSettings
Tasks []*task
}
type tasksStore map[string]*task
type configLoader struct {
m sync.RWMutex
configFile string
settings clientSettings
store tasksStore
mtime time.Time
stop chan bool
log echo.Logger
}
func newConfigLoader(configFile string, log echo.Logger) (*configLoader, error) {
var l = &configLoader{
configFile: configFile,
store: make(tasksStore),
stop: make(chan bool),
log: log,
}
err := l.reload()
go l.periodicReload()
return l, err
}
func (l *configLoader) periodicReload() {
t := time.NewTicker(1 * time.Minute)
defer t.Stop()
for {
select {
case <-t.C:
stat, err := os.Stat(l.configFile)
if err != nil {
l.log.Error("Failed to get stat for %s: %v", l.configFile, err)
}
if stat.ModTime().After(l.mtime) {
if err := l.reload(); err != nil {
l.log.Error("Failed to reload %s: %v", l.configFile, err)
}
}
case <-l.stop:
return
}
}
}
func (l *configLoader) load() (*clientSettings, tasksStore, error) {
bytes, err := ioutil.ReadFile(l.configFile)
if err != nil {
return nil, nil, err
}
var c clientConfig
c.Settings.Gzip = true
if err := yaml.Unmarshal(bytes, &c); err != nil {
return nil, nil, err
}
store := make(tasksStore)
for idx, t := range c.Tasks {
if t.Name == "" {
return nil, nil, errors.Errorf("Task idx=%d has no name", idx)
}
if t.Interval != "" {
d, err := time.ParseDuration(t.Interval)
if err != nil {
return nil, nil, errors.Wrap(err, t.Name+".Interval")
}
if d < 100*time.Millisecond {
return nil, nil, errors.New(t.Name + ".Interval too small (min 100ms): " + t.Interval)
}
t.intervalDuration = d
}
if t.Splice != "" {
d, err := time.ParseDuration(t.Splice)
if err != nil {
return nil, nil, errors.Wrap(err, t.Name+".Splice")
}
if d < 10*time.Millisecond {
return nil, nil, errors.New(t.Name + ".Splice too small (min 10ms): " + t.Splice)
}
t.spliceDuration = d
}
if t.Timeout != "" {
d, err := time.ParseDuration(t.Timeout)
if err != nil {
return nil, nil, errors.Wrap(err, t.Name+".Timeout")
}
if t.Interval != "" && d > t.intervalDuration {
return nil, nil, errors.New(t.Name + ".Timeout > " + t.Name + ".Interval")
}
t.timeoutDuration = d
} else {
if t.Interval != "" {
t.timeoutDuration = t.intervalDuration
} else {
t.timeoutDuration = defaultTimeout
}
}
store[t.Name] = t
}
return &c.Settings, store, nil
}
func (l *configLoader) reload() error {
s, store, err := l.load()
if err != nil {
return err
}
l.m.Lock()
l.settings = *s
l.store = store
l.mtime = time.Now()
l.m.Unlock()
return nil
}
func (l *configLoader) tasksList() string {
var buf bytes.Buffer
l.m.RLock()
for k := range l.store {
buf.WriteString(k + "\n")
}
l.m.RUnlock()
return buf.String()
}
func (l *configLoader) lookupTask(taskName string) (*task, bool) {
l.m.RLock()
t, ok := l.store[taskName]
l.m.RUnlock()
return t, ok
}