This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_impl.go
178 lines (158 loc) · 4.2 KB
/
pool_impl.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
package service
import (
"context"
"sync"
"github.com/containerssh/log"
)
type pool struct {
mutex *sync.Mutex
services []Service
lifecycles map[Service]Lifecycle
serviceStates map[Service]State
lifecycleFactory LifecycleFactory
running bool
startupComplete chan struct{}
stopComplete chan struct{}
lastError error
stopping bool
logger log.Logger
}
func (p *pool) String() string {
return "Service Pool"
}
func (p *pool) Add(s Service) Lifecycle {
p.mutex.Lock()
if p.running {
panic("bug: pool already running, cannot add service")
}
defer p.mutex.Unlock()
l := p.lifecycleFactory.Make(s)
l.OnStateChange(p.onServiceStateChange)
p.serviceStates[s] = StateStopped
p.services = append(p.services, s)
p.lifecycles[s] = l
return l
}
func (p *pool) RunWithLifecycle(lifecycle Lifecycle) error {
p.mutex.Lock()
if p.running {
p.mutex.Unlock()
panic("bug: pool already running, cannot run again")
}
p.logger.Info(log.NewMessage(MServicesStarting, "Services are starting..."))
p.stopComplete = make(chan struct{}, len(p.services))
p.running = true
p.stopping = false
p.mutex.Unlock()
defer func() {
p.mutex.Lock()
p.running = false
p.mutex.Unlock()
}()
for _, service := range p.services {
p.runService(service)
}
stopped := false
startedServices := len(p.services)
finished := false
for i := 0; i < len(p.services); i++ {
select {
case <-p.startupComplete:
case <-p.stopComplete:
stopped = true
startedServices--
finished = true
}
if finished {
break
}
}
startedServices = p.processRunning(lifecycle, stopped, startedServices)
for i := 0; i < startedServices; i++ {
<-p.stopComplete
}
p.logger.Info(log.NewMessage(MServicesStopped, "All services have stopped."))
return p.lastError
}
func (p *pool) processRunning(lifecycle Lifecycle, stopped bool, startedServices int) int {
if !stopped {
p.logger.Info(log.NewMessage(MServicesRunning, "All services are now running."))
lifecycle.Running()
select {
case <-p.stopComplete:
// One service stopped, initiate shutdown
p.logger.Info(log.NewMessage(MServicesStopping, "Services are now stopping..."))
startedServices--
case <-lifecycle.Context().Done():
p.logger.Info(log.NewMessage(MServicesStopping, "Services are now stopping..."))
lifecycle.Stopping()
p.triggerStop(lifecycle.ShutdownContext())
}
} else {
p.logger.Info(log.NewMessage(MServicesStopping, "Services are now stopping..."))
lifecycle.Stopping()
p.triggerStop(context.Background())
}
return startedServices
}
func (p *pool) runService(service Service) {
go func() {
_ = p.lifecycles[service].Run()
}()
}
func (p *pool) onServiceStateChange(s Service, l Lifecycle, newState State) {
if s == p {
return
}
p.mutex.Lock()
oldState := p.serviceStates[s]
p.serviceStates[s] = newState
p.mutex.Unlock()
if oldState == newState {
return
}
switch newState {
case StateStarting:
p.logger.Info(log.NewMessage(MServiceStarting, "%s is starting...", s.String()).Label("service", s.String()))
return
case StateRunning:
p.logger.Info(log.NewMessage(MServiceRunning, "%s is running.", s.String()).Label("service", s.String()))
select {
case p.startupComplete <- struct{}{}:
default:
}
case StateStopping:
p.logger.Info(log.NewMessage(MServiceStopping, "%s is stopping...", s.String()).Label("service", s.String()))
p.triggerStop(context.Background())
case StateStopped:
p.logger.Info(log.NewMessage(MServiceStopped, "%s has stopped.", s.String()).Label("service", s.String()))
p.triggerStop(context.Background())
p.stopComplete <- struct{}{}
case StateCrashed:
p.logger.Error(log.NewMessage(EServiceCrashed, "%s has crashed.", s.String()).Label("service", s.String()))
p.lastError = l.Error()
p.triggerStop(context.Background())
p.stopComplete <- struct{}{}
}
}
func (p *pool) triggerStop(shutdownContext context.Context) {
p.mutex.Lock()
if p.stopping {
p.mutex.Unlock()
return
}
p.stopping = true
svc := p.services
p.mutex.Unlock()
wg := &sync.WaitGroup{}
wg.Add(len(svc))
for _, s := range svc {
service := s
l := p.lifecycles[service]
go func() {
defer wg.Done()
l.Stop(shutdownContext)
}()
}
wg.Wait()
}