-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
331 lines (270 loc) · 7.49 KB
/
services.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/WiseGrowth/go-wisebot/led"
"github.com/WiseGrowth/go-wisebot/logger"
"github.com/WiseGrowth/wisebot-operator/command"
"github.com/WiseGrowth/wisebot-operator/git"
)
const (
maxRetries = 3
)
// Service encapsulates a command an its repository
type Service struct {
Name string
cmd *command.Command
repo *git.Repo
finished chan error // command error
stop chan struct{} // stop command watcher
sync.Mutex // guards Update and Bootstrap functions.
}
func newService(name string, c *command.Command, r *git.Repo) *Service {
svc := &Service{Name: name, cmd: c, repo: r}
svc.finished = make(chan error, 1)
c.Finish = svc.finished
return svc
}
// MarshalJSON implements json marshal interface
func (s *Service) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Name string `json:"name"`
Version string `json:"version"`
Status command.Status `json:"status"`
RepoVersion string `json:"repo_version"`
}{
Name: s.Name,
Version: s.cmd.Version,
Status: s.cmd.Status(),
RepoVersion: s.repo.CurrentHead(),
})
}
// Start runs the service observe function in background and then proxies the
// Start function call to its internal command struct.
func (s *Service) Start() error {
go s.observe()
return s.cmd.Start()
}
// Stop proxies function to the its command.
func (s *Service) Stop() error {
return s.cmd.Stop()
}
// Update proxies function to the its command.
func (s *Service) Update() (bool, error) {
s.Lock()
defer s.Unlock()
s.cmd.SetStatus(command.StatusUpdating)
return s.cmd.Update(s.repo)
}
// Bootstrap proxies function to the its repo.
func (s *Service) Bootstrap(update bool) error {
s.Lock()
defer s.Unlock()
if err := s.repo.Bootstrap(update); err != nil {
return err
}
s.cmd.Version = s.repo.CurrentHead()
return nil
}
// observe observes if the internal service command exited with error or not.
// If the command exited with error, it notifies the led service.
func (s *Service) observe() {
log := s.logger()
log.Info("Start observing")
running := true
for running {
select {
case err := <-s.finished:
if err != nil {
log.Debugf("Service exited with error: %s\n", err.Error())
go notifyServiceExitErrorWithRetry(s)
}
running = false
}
}
log.Info("Stop observing")
}
// notifyInternetWithRetry calls led.PostServiceExitError until exits without
// error. The retry interval is 3 seconds.
func notifyServiceExitErrorWithRetry(s *Service) {
now := time.Now()
log := s.logger()
try := 0
for {
if try == maxRetries {
log.Debug("max service exited error post retries reached")
break
}
if err := led.PostServiceExitError(s.Name, now); err != nil {
log.Error(err)
time.Sleep(3 * time.Second)
log.Debug("service exited error post failed, retrying")
try++
continue
}
log.Debug("service exited error posted!")
break
}
}
// ServiceStore represents a set of commands. It has convinient methods to run
// and stop all commands.
type ServiceStore struct {
mu sync.RWMutex
list map[string]*Service
}
// MarshalJSON implements json marshal interface
func (ss *ServiceStore) MarshalJSON() ([]byte, error) {
svcs := make([]*Service, len(ss.list))
i := 0
for _, svc := range ss.list {
ss.mu.RLock()
svcs[i] = svc
i++
ss.mu.RUnlock()
}
return json.Marshal(svcs)
}
func (s *Service) logger() *logrus.Entry {
return logger.GetLogger().WithFields(logrus.Fields{
"name": s.Name,
"command_version": s.cmd.Version,
"status": s.cmd.Status(),
"repo_version": s.repo.CurrentHead(),
})
}
// Find looks the service in the list by its name. If the service does not
// exists, it returns a nil Service and a false value.
func (ss *ServiceStore) Find(name string) (svc *Service, ok bool) {
ss.mu.RLock()
defer ss.mu.RUnlock()
svc, ok = ss.list[name]
return svc, ok
}
// Bootstrap loops each service in the list and calls the bootstrap function.
func (ss *ServiceStore) Bootstrap(update bool) error {
ss.mu.RLock()
defer ss.mu.RUnlock()
for _, svc := range ss.list {
if err := svc.Bootstrap(update); err != nil {
return err
}
}
return nil
}
// Update search the given command in the map and runs its Update function. If
// the command is not found, an error is returned.
func (ss *ServiceStore) Update(name string) error {
svc, ok := ss.Find(name)
if !ok {
return fmt.Errorf("services: service with name %q not found", name)
}
cmd := svc.cmd
svc.logger().Info("Running update")
oldStatus := svc.cmd.Status()
updated, err := svc.Update()
if err != nil {
svc.logger().Debug("Error when updating")
svc.cmd.SetStatus(oldStatus)
return err
}
if !updated {
svc.logger().Info("No new updates")
svc.cmd.SetStatus(oldStatus)
return nil
}
svc.logger().Info("Update found, stopping")
if err := cmd.Stop(); err != nil {
return err
}
cmd = cmd.Clone()
ss.Save(svc.Name, cmd, svc.repo)
svc.logger().Info("Starting updated service")
if err := cmd.Start(); err != nil {
return err
}
return nil
}
// Save builds and add the service to the list.
func (ss *ServiceStore) Save(name string, c *command.Command, r *git.Repo) *Service {
s := newService(name, c, r)
ss.mu.RLock()
if ss.list == nil {
ss.list = make(map[string]*Service)
}
ss.mu.RUnlock()
ss.mu.Lock()
defer ss.mu.Unlock()
ss.list[s.Name] = s
return s
}
// StartService starts a specific service inside the store. If the service is
// not found in the list, it returns an error.
func (ss *ServiceStore) StartService(name string) error {
svc, ok := ss.Find(name)
if !ok {
return fmt.Errorf("services: service %q not found for starting", name)
}
status := svc.cmd.Status()
if status == command.StatusCrashed || status == command.StatusBootingError || status == command.StatusStopped || status == command.StatusDone {
newCmd := svc.cmd.Clone()
svc = ss.Save(svc.Name, newCmd, svc.repo)
}
svc.logger().Info("Starting")
return svc.Start()
}
// Start starts all the commands inside the command list by looping and calling
// each command Start function.
func (ss *ServiceStore) Start() error {
ss.mu.RLock()
defer ss.mu.RUnlock()
for _, svc := range ss.list {
svc.logger().Info("Starting")
if err := svc.Start(); err != nil {
return err
}
}
return nil
}
// StopService stops a specific service inside the store. If the service is not
// found in the list, it returns an error.
func (ss *ServiceStore) StopService(name string) error {
svc, ok := ss.Find(name)
if !ok {
return fmt.Errorf("services: service %q not found for stopping", name)
}
svc.logger().Info("Stopping")
defer svc.logger().Info("Stopped")
return svc.Stop()
}
// RestartService restarts a specific service inside the store. If the service is not
// found in the list, it returns an error.
func (ss *ServiceStore) RestartService(name string) error {
svc, ok := ss.Find(name)
if !ok {
return fmt.Errorf("services: service with name %q not found", name)
}
svc.logger().Info("Restarting")
if err := svc.Stop(); err != nil {
return err
}
cmd := svc.cmd.Clone()
ss.Save(svc.Name, cmd, svc.repo)
defer svc.logger().Info("Restarted")
return cmd.Start()
}
// Stop stops all the services inside the store by looping and calling each
// service command's Stop function.
func (ss *ServiceStore) Stop() error {
ss.mu.RLock()
defer ss.mu.RUnlock()
for _, svc := range ss.list {
svc.logger().Info("Stopping")
if err := svc.Stop(); err != nil {
return err
}
}
return nil
}