-
Notifications
You must be signed in to change notification settings - Fork 789
/
module_service.go
96 lines (78 loc) · 2.95 KB
/
module_service.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
package util
import (
"context"
"fmt"
"github.com/go-kit/kit/log/level"
"github.com/pkg/errors"
"github.com/cortexproject/cortex/pkg/util/services"
)
// This service wraps module service, and adds waiting for dependencies to start before starting,
// and dependant modules to stop before stopping this module service.
type moduleService struct {
services.Service
service services.Service
name string
// startDeps, stopDeps return map of service names to services
startDeps, stopDeps func(string) map[string]services.Service
}
// NewModuleService wraps a module service, and makes sure that dependencies are started/stopped before module service starts or stops.
// If any dependency fails to start, this service fails as well.
// On stop, errors from failed dependencies are ignored.
func NewModuleService(name string, service services.Service, startDeps, stopDeps func(string) map[string]services.Service) services.Service {
w := &moduleService{
name: name,
service: service,
startDeps: startDeps,
stopDeps: stopDeps,
}
w.Service = services.NewBasicService(w.start, w.run, w.stop)
return w
}
func (w *moduleService) start(serviceContext context.Context) error {
// wait until all startDeps are running
startDeps := w.startDeps(w.name)
for m, s := range startDeps {
if s == nil {
continue
}
level.Debug(Logger).Log("msg", "module waiting for initialization", "module", w.name, "waiting_for", m)
err := s.AwaitRunning(serviceContext)
if err != nil {
return fmt.Errorf("failed to start %v, because it depends on module %v, which has failed: %w", w.name, m, err)
}
}
// we don't want to let this service to stop until all dependant services are stopped,
// so we use independent context here
level.Info(Logger).Log("msg", "initialising", "module", w.name)
err := w.service.StartAsync(context.Background())
if err != nil {
return errors.Wrapf(err, "error starting module: %s", w.name)
}
return w.service.AwaitRunning(serviceContext)
}
func (w *moduleService) run(serviceContext context.Context) error {
// wait until service stops, or context is canceled, whatever happens first.
// We don't care about exact error here
_ = w.service.AwaitTerminated(serviceContext)
return w.service.FailureCase()
}
func (w *moduleService) stop(_ error) error {
// wait until all stopDeps have stopped
stopDeps := w.stopDeps(w.name)
for _, s := range stopDeps {
if s == nil {
continue
}
// Passed context isn't canceled, so we can only get error here, if service
// fails. But we don't care *how* service stops, as long as it is done.
_ = s.AwaitTerminated(context.Background())
}
level.Debug(Logger).Log("msg", "stopping", "module", w.name)
err := services.StopAndAwaitTerminated(context.Background(), w.service)
if err != nil && err != ErrStopProcess {
level.Warn(Logger).Log("msg", "error stopping module", "module", w.name, "err", err)
} else {
level.Info(Logger).Log("msg", "module stopped", "module", w.name)
}
return err
}