-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
64 lines (49 loc) · 1.11 KB
/
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
package readyness
import (
"sync"
"github.com/fraym/golog"
)
type Service struct {
components map[string]bool
logger golog.Logger
mux sync.RWMutex
}
func New(logger golog.Logger) *Service {
return &Service{
components: make(map[string]bool),
logger: logger,
}
}
func (s *Service) isReady() (bool, string) {
for component, isReady := range s.components {
if !isReady {
return false, component
}
}
return true, ""
}
func (s *Service) IsReady(component ...string) (bool, string) {
s.mux.RLock()
defer s.mux.RUnlock()
if len(component) == 0 {
return s.isReady()
}
for _, c := range component {
if ready, ok := s.components[c]; !ok || !ready {
return false, c
}
}
return true, ""
}
func (s *Service) SetReady(component string) {
s.mux.Lock()
defer s.mux.Unlock()
s.components[component] = true
s.logger.Info().WithField("component", component).Write("Component is ready")
}
func (s *Service) Register(component string) {
s.mux.Lock()
defer s.mux.Unlock()
s.components[component] = false
s.logger.Info().WithField("component", component).Write("Registered unready component")
}