-
Notifications
You must be signed in to change notification settings - Fork 1
/
supervisor.go
244 lines (190 loc) · 4.27 KB
/
supervisor.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"os"
"path/filepath"
"regexp"
"strings"
)
var (
svcPrefix = regexp.MustCompile(`^\d.\-`)
)
type Supervisor struct {
Config Config
dir string
groupsServices map[string][]string
services map[string]*Service
}
type ConfigParseError struct {
errors map[string]error
}
func (c *ConfigParseError) Append(svc string, err error) {
if c.errors == nil {
c.errors = make(map[string]error)
}
c.errors[svc] = err
}
func (c ConfigParseError) Error() string {
out := new(strings.Builder)
out.WriteString("the following error(s) occurred parsing configs:\n")
for svc, err := range c.errors {
out.WriteString(svc + ": " + err.Error() + "\n")
}
return out.String()
}
func New(dir string) (s *Supervisor, err error) {
s = &Supervisor{
dir: dir,
}
err = s.LoadConfigs()
return
}
func (s *Supervisor) LoadConfigs() (err error) {
s.Config, err = LoadConfig(filepath.Join(s.dir, ".config.toml"))
if err != nil {
return
}
groupsServices := make(map[string][]string)
services := make(map[string]*Service)
entries, err := os.ReadDir(s.dir)
if err != nil {
return
}
var svc *Service
cpe := ConfigParseError{}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
name := serviceName(entry.Name())
svc, err = LoadService(name, filepath.Join(s.dir, entry.Name()))
if err != nil {
cpe.Append(entry.Name(), err)
svc.loadError = err.Error()
}
var groupName string
if svc.loadError == "" {
groupName = s.Config.ReconcileOverride(name, svc.Config.Grouping.GroupName)
} else {
groupName = ""
}
_, ok := groupsServices[groupName]
if !ok {
groupsServices[groupName] = make([]string, 0)
}
groupsServices[groupName] = append(groupsServices[groupName], name)
// If this service already exists/ has some state then copy it over
// (so we don't lose running state)
oldSvc := s.services[name]
if oldSvc != nil {
svc.status = oldSvc.status
}
services[name] = svc
}
s.groupsServices = groupsServices
s.services = services
if len(cpe.errors) > 0 {
err = cpe
}
return
}
func (s *Supervisor) Start(name string, wait bool) error {
svc, ok := s.services[name]
if !ok {
return errServiceNotExist
}
if svc.loadError != "" {
return errServiceDodgyConf
}
return svc.Start(wait)
}
func (s *Supervisor) Status(name string) (ServiceStatus, error) {
svc, ok := s.services[name]
if !ok {
return ServiceStatus{}, errServiceNotExist
}
return svc.Status()
}
func (s *Supervisor) Stop(name string) error {
svc, ok := s.services[name]
if !ok {
return errServiceNotExist
}
return svc.Stop()
}
func (s *Supervisor) Reload(name string) error {
svc, ok := s.services[name]
if !ok {
return errServiceNotExist
}
return svc.Reload()
}
func (s *Supervisor) StartAll() {
var err error
for _, group := range s.Config.Groups {
// Ignore anything with an empty group; this signifies
// a config error
if group == "" {
continue
}
services, ok := s.groupsServices[group]
if !ok {
sugar.Errorw("group either has no services or does not exist",
"group", group,
)
continue
}
for _, service := range services {
sugar.Infow("starting",
"group", group,
"service", service,
)
err = s.Start(service, true)
if err != nil {
sugar.Errorw("failed!",
"group", group,
"service", service,
"error", err.Error(),
)
continue
}
sugar.Infow("started!",
"group", group,
"service", service,
)
}
}
}
// StopAll does the opposite of StartAll; it reverses the order of
// s.Config.Groups, then reverses the order of those services in order
// to stop them all
func (s *Supervisor) StopAll() (err error) {
var svc *Service
for _, group := range reverse(s.Config.Groups) {
for _, svcName := range reverse(s.groupsServices[group]) {
svc = s.services[svcName]
if svc == nil || !svc.isRunning() {
continue
}
err = s.Stop(svcName)
if err != nil {
return
}
}
}
return
}
func serviceName(s string) string {
if !svcPrefix.Match([]byte(s)) {
return s
}
return svcPrefix.ReplaceAllString(s, "")
}
func reverse(in []string) (out []string) {
out = make([]string, len(in))
copy(out, in)
for i := len(out)/2 - 1; i >= 0; i-- {
opp := len(out) - 1 - i
out[i], out[opp] = out[opp], out[i]
}
return
}