This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
services.go
275 lines (240 loc) · 7.37 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
// Copyright (c) 2021 6 River Systems
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package registry
import (
"context"
"errors"
"fmt"
"time"
"golang.org/x/sync/errgroup"
"go.6river.tech/gosix/ent"
"go.6river.tech/gosix/logging"
)
type ServiceTag int
// AddService registers a Service instance to be affected by future calls to
// InitializeAll, StartAll, StopAll, and CleanupAll. It is an error (panic) to
// call AddService when StartAll is running.
func (r *Registry) AddService(s Service) ServiceTag {
r.svcMu.Lock()
defer r.svcMu.Unlock()
if r.cancelRunning != nil {
panic(errors.New("Cannot add services after they have been started"))
}
tag := len(r.allServices)
r.allServices = append(r.allServices, s)
r.allReadies = append(r.allReadies, make(chan struct{}))
return ServiceTag(tag)
}
// we don't create a logger as a package-level variable, because it would get
// initialized before logging is configured
func (r *Registry) logger() *logging.Logger {
r._loggerOnce.Do(func() {
// TODO: registry names
r._logger = logging.GetLogger("services/registry")
})
return r._logger
}
// InitializeServices calls the Initialize method on all registered services, in
// parallel in goroutines.
func (r *Registry) InitializeServices(ctx context.Context, client ent.EntClientBase) error {
if r.initialized {
return errors.New("Cannot re-initialize services without cleanup")
}
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
eg, egCtx := errgroup.WithContext(cancelCtx)
r.logger().Info().Msgf("Initializing %d services", len(r.allServices))
for _, s := range r.allServices {
s := s
eg.Go(func() error {
return s.Initialize(egCtx, r, client)
})
}
if err := eg.Wait(); err != nil {
err2 := r.CleanupServices(ctx)
// TODO: error during error recovery, yuck
r.logger().Error().Err(err2).Msg("Failed to cleanup after failure to initialize")
return err
}
r.initialized = true
return nil
}
func (r *Registry) ServicesInitialized() bool {
return r.initialized
}
// StartServices starts all registered services in individual goroutines. It is an
// error to call StartServices if it is already running. StartServices returns once all
// services are started, it does not wait for them to complete. Use StopAll to
// request they stop and wait for the result.
func (r *Registry) StartServices(ctx context.Context) error {
if !r.initialized {
return errors.New("Cannot start without initializing first")
}
if r.ServicesStarted() || r.cancelRunning != nil {
return errors.New("Cannot start again when services are already running")
}
var cancelCtx context.Context
cancelCtx, r.cancelRunning = context.WithCancel(ctx)
r.runningGroup, r.runningCtx = errgroup.WithContext(cancelCtx)
r.logger().Info().Msgf("Starting %d services", len(r.allServices))
for i, s := range r.allServices {
s := s
ready := r.allReadies[i]
r.runningGroup.Go(func() error {
return s.Start(r.runningCtx, ready)
})
}
r.svcMu.Lock()
defer r.svcMu.Unlock()
r.started = true
return nil
}
func (r *Registry) ServicesStarted() bool {
r.svcMu.Lock()
defer r.svcMu.Unlock()
return r.started
}
func (r *Registry) ReadyWaiter(tag ServiceTag) <-chan struct{} {
r.svcMu.Lock()
defer r.svcMu.Unlock()
return r.allReadies[tag]
}
func (r *Registry) WaitAllReady(ctx context.Context) error {
if !r.ServicesStarted() {
panic(errors.New("Cannot wait for services to be ready until they have been started"))
}
for i := range r.allReadies {
if err := r.waitReady(ctx, ServiceTag(i)); err != nil {
return err
}
}
return nil
}
var ServiceNotFoundError = fmt.Errorf("service not found")
func (r *Registry) WaitReadyByName(ctx context.Context, name string) error {
if !r.ServicesStarted() {
panic(errors.New("Cannot wait for services to be ready until they have been started"))
}
found := false
for i := range r.allReadies {
if r.allServices[i].Name() != name {
continue
}
found = true
if err := r.waitReady(ctx, ServiceTag(i)); err != nil {
return err
}
}
if !found {
return ServiceNotFoundError
}
return nil
}
func (r *Registry) waitReady(ctx context.Context, tag ServiceTag) error {
s := r.allServices[tag]
ready := r.allReadies[tag]
for {
select {
case <-ctx.Done():
err := ctx.Err()
if errors.Is(err, context.DeadlineExceeded) {
r.logger().Error().
Int("serviceTag", int(tag)).
Str("service", s.Name()).
Msg("Timed out waiting for service to be ready")
}
return err
case <-r.runningCtx.Done():
err := r.runningGroup.Wait()
if err != nil {
r.logger().Error().
Err(err).
Int("serviceTag", int(tag)).
Str("service", s.Name()).
Msg("Services failed while waiting for service to be ready")
}
return err
case <-time.After(time.Second):
r.logger().Warn().
Int("serviceTag", int(tag)).
Str("service", s.Name()).
Msg("Service is slow to get ready")
case <-ready:
return nil
}
}
}
// WaitServices will wait for the running services, if any, to all end. It will
// return the resulting error, if any.
func (r *Registry) WaitServices() error {
if r.runningGroup == nil {
return nil
}
err := r.runningGroup.Wait()
if err == context.Canceled || err == context.DeadlineExceeded {
// this just means yes, we stopped it
err = nil
}
// flag that services are no longer running
r.svcMu.Lock()
defer r.svcMu.Unlock()
r.started = false
return err
}
// RequestStopServices requests all running services stop, by cancelling their
// Context objects
func (r *Registry) RequestStopServices() {
r.svcMu.Lock()
defer r.svcMu.Unlock()
if !r.initialized {
return
}
if r.cancelRunning != nil {
r.logger().Info().Msgf("Stopping %d services", len(r.allServices))
r.cancelRunning()
}
}
func (r *Registry) CleanupServices(ctx context.Context) error {
if !r.initialized {
return nil
}
if r.ServicesStarted() {
return errors.New("Cannot cleanup when services are still running")
}
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
eg, egCtx := errgroup.WithContext(cancelCtx)
r.logger().Info().Msgf("Cleaning up %d services", len(r.allServices))
for _, s := range r.allServices {
s := s
eg.Go(func() error {
return s.Cleanup(egCtx, r)
})
}
err := eg.Wait()
if err == context.Canceled || err == context.DeadlineExceeded {
// this just means yes, we stopped it
err = nil
}
r.svcMu.Lock()
defer r.svcMu.Unlock()
r.cancelRunning = nil
r.initialized = false
return err
}