-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchief.go
More file actions
384 lines (329 loc) · 10.4 KB
/
chief.go
File metadata and controls
384 lines (329 loc) · 10.4 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package uwe
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/lancer-kit/uwe/v3/socket"
"github.com/sheb-gregor/sam"
)
// Chief is a supervisor that can be placed at the top of the go app's execution stack,
// it is blocked until SIGTERM is intercepted, and then it shut down all workers gracefully.
// Also, `Chief` can be used as a child supervisor inside the `Worker`,
// which is launched by `Chief` at the top-level.
type Chief interface {
// AddWorker registers the worker in the pool.
AddWorker(WorkerName, Worker, ...WorkerOpts) Chief
AddWorkerAndLaunch(WorkerName, Worker, ...WorkerOpts) Chief
// GetWorkersStates returns the current state of all registered workers.
GetWorkersStates() map[WorkerName]sam.State
// EnableServiceSocket initializes `net.Socket` server for internal management purposes.
// By default, includes two actions:
// - "status" is a healthcheck-like, because it returns status of all workers;
// - "ping" is a simple command that returns the "pong" message.
// The user can provide his own list of actions with handler closures.
EnableServiceSocket(app AppInfo, actions ...socket.Action) Chief
// Event returns the channel with internal Events.
// > ATTENTION:
// `Event() <-chan Event` and `SetEventHandler(EventHandler)`
// are mutually exclusive, but one of them must be used!
Event() <-chan Event
// SetEventHandler adds a callback that processes the `Chief`
// internal events and can log them or do something else.
// > ATTENTION:
// `Event() <-chan Event` and `SetEventHandler(EventHandler)`
// are mutually exclusive, but one of them must be used!
SetEventHandler(EventHandler) Chief
// SetContext replaces the default context with the provided one.
// It can be used to deliver some values inside `(Worker) .Run (ctx Context)`.
SetContext(context.Context) Chief
// SetLocker sets a custom `Locker`, if it is not set,
// the default `Locker` will be used, which expects SIGTERM or SIGINT system signals.
SetLocker(Locker) Chief
// SetShutdown sets `Shutdown` callback.
SetShutdown(Shutdown) Chief
// SetForceStopTimeout replaces the `DefaultForceStopTimeout`.
// ForceStopTimeout is the duration before
// the worker will be killed if it wouldn't finish Run after the stop signal.
SetForceStopTimeout(time.Duration) Chief
// UseCustomIMQBroker sets non-standard implementation
// of the IMQBroker to replace default one.
UseCustomIMQBroker(IMQBroker) Chief
// UseNopIMQBroker replaces default IMQ Broker by empty stub.
// NOP stands for no-operations.
UseNopIMQBroker() Chief
// Run is the main entry point into the `Chief` run loop.
// This method initializes all added workers, the server `net.Socket`,
// if enabled, starts the workers in separate routines
// and waits for the end of lock produced by the locker function.
Run()
// Shutdown sends stop signal to all child goroutines
// by triggering of the `context.CancelFunc()` and
// executes `Shutdown` callback.
Shutdown()
}
type (
// Locker is a function whose completion of
// a call is a signal to stop `Chief` and all workers.
Locker func()
// Recover is a function that will be used as a
// `defer call` to handle each worker's panic.
Recover func(name WorkerName)
// Shutdown is a callback function that will be executed after the Chief
// and workers are stopped. Its main purpose is to close, complete,
// or retain some global states or shared resources.
Shutdown func()
// EventHandler callback that processes the `Chief` internal events,
// can log them or do something else.
EventHandler func(Event)
)
// DefaultForceStopTimeout is a timeout for killing all workers.
const DefaultForceStopTimeout = 45 * time.Second
type chief struct {
ctx context.Context
cancel context.CancelFunc
forceStopTimeout time.Duration
locker Locker
shutdown Shutdown
wPool *workerPool
rtWorkersLaunched bool
rtWorkersMutex sync.Mutex
rtWorkersWG sync.WaitGroup
rtWorkersCtx context.Context
eventMutexLocked bool
eventMutex sync.Mutex
eventChan chan Event
eventHandler EventHandler
broker IMQBroker
sw *socket.Server
}
// NewChief returns new instance of standard `Chief` implementation.
func NewChief() Chief {
c := &chief{
eventChan: make(chan Event),
forceStopTimeout: DefaultForceStopTimeout,
wPool: &workerPool{
workers: make(map[WorkerName]*workerRO),
},
}
c.ctx, c.cancel = context.WithCancel(context.Background())
return c
}
// EnableServiceSocket initializes `net.Socket` server for internal management purposes.
// By default, includes two actions:
// - "status" is a command useful for health-checks, because it returns status of all workers;
// - "ping" is a simple command that returns the "pong" message.
//
// The user can provide his own list of actions with handler closures.
func (c *chief) EnableServiceSocket(app AppInfo, actions ...socket.Action) Chief {
statusAction := socket.Action{Name: StatusAction,
Handler: func(_ socket.Request) socket.Response {
return socket.NewResponse(socket.StatusOk,
StateInfo{App: app, Workers: c.wPool.getWorkersStates()}, "")
},
}
pingAction := socket.Action{Name: PingAction,
Handler: func(_ socket.Request) socket.Response {
return socket.NewResponse(socket.StatusOk, "pong", "")
},
}
actions = append(actions, statusAction, pingAction)
c.sw = socket.NewServer(app.SocketName(), actions...)
return c
}
// AddWorker registers the worker in the pool.
func (c *chief) AddWorker(name WorkerName, worker Worker, opts ...WorkerOpts) Chief {
if err := c.wPool.setWorker(name, worker, opts); err != nil {
c.eventChan <- ErrorEvent(err.Error()).SetWorker(name)
}
return c
}
func (c *chief) AddWorkerAndLaunch(name WorkerName, worker Worker, opts ...WorkerOpts) Chief {
if err := c.wPool.setWorker(name, worker, opts); err != nil {
c.eventChan <- ErrorEvent(err.Error()).SetWorker(name)
}
if c.rtWorkersLaunched {
c.rtWorkersMutex.Lock()
c.launchWorker(name)
c.rtWorkersMutex.Unlock()
}
return c
}
// GetWorkersStates returns the current state of all registered workers.
func (c *chief) GetWorkersStates() map[WorkerName]sam.State {
return c.wPool.getWorkersStates()
}
// SetEventHandler adds a callback that processes the `Chief`
// internal events and can log them or do something else.
func (c *chief) SetEventHandler(handler EventHandler) Chief {
c.eventHandler = handler
return c
}
// SetContext replaces the default context with the provided one.
// It can be used to deliver some values inside `(Worker).Run(ctx Context)`.
func (c *chief) SetContext(ctx context.Context) Chief {
c.ctx = ctx
return c
}
// SetLocker sets a custom `Locker`, if it is not set,
// the default `Locker` will be used, which expects SIGTERM or SIGINT system signals.
func (c *chief) SetLocker(locker Locker) Chief {
c.locker = locker
return c
}
func (c *chief) UseCustomIMQBroker(broker IMQBroker) Chief {
c.broker = broker
return c
}
func (c *chief) UseNopIMQBroker() Chief {
c.broker = &NopBroker{}
return c
}
// SetShutdown sets `Shutdown` callback.
func (c *chief) SetShutdown(shutdown Shutdown) Chief {
c.shutdown = shutdown
return c
}
// SetForceStopTimeout replaces the `DefaultForceStopTimeout`.
func (c *chief) SetForceStopTimeout(forceStopTimeout time.Duration) Chief {
c.forceStopTimeout = forceStopTimeout
return c
}
// Event returns the channel with internal Events.
func (c *chief) Event() <-chan Event {
if c.eventHandler != nil {
return nil
}
c.eventMutexLocked = true
c.eventMutex.Lock()
return c.eventChan
}
// Run is the main entry point into the `Chief` run loop.
// This method initializes all added workers, the server `net.Socket`,
// if enabled, starts the workers in separate goroutines
// and waits for the end of lock produced by the locker function.
func (c *chief) Run() {
if c.locker == nil {
c.locker = waitForSignal
}
if c.eventHandler != nil {
stop := make(chan struct{})
defer func() {
stop <- struct{}{}
}()
go c.handleEvents(stop)
}
c.run()
}
// Shutdown sends stop signal to all child goroutines
// by triggering of the `context.CancelFunc()`
// and executes `Shutdown` callback.
func (c *chief) Shutdown() {
c.cancel()
if c.eventMutexLocked {
c.eventMutex.Unlock()
}
if c.shutdown != nil {
c.shutdown()
}
}
func (c *chief) run() {
lockerDone := make(chan struct{})
go func() {
c.locker()
c.Shutdown()
lockerDone <- struct{}{}
}()
poolStopped := make(chan struct{})
go func() {
err := c.runPool()
if err != nil {
c.eventChan <- ErrorEvent(err.Error())
lockerDone <- struct{}{}
}
poolStopped <- struct{}{}
}()
<-lockerDone
select {
case <-poolStopped:
return
case <-time.NewTimer(c.forceStopTimeout).C:
c.eventChan <- ErrorEvent("graceful shutdown failed")
return
}
}
func (c *chief) handleEvents(stop <-chan struct{}) {
c.eventMutexLocked = true
c.eventMutex.Lock()
for {
select {
case event := <-c.eventChan:
c.eventHandler(event)
case <-stop:
return
}
}
}
func (c *chief) runPool() error {
c.rtWorkersWG = sync.WaitGroup{}
var runCount int
ctx, cancel := context.WithCancel(c.ctx)
c.rtWorkersCtx = ctx
c.rtWorkersLaunched = true
if c.broker == nil {
c.broker = NewBroker(len(c.wPool.workers) * 4)
}
if err := c.broker.Init(); err != nil {
cancel()
return fmt.Errorf("unable to init imq broker: %w", err)
}
for _, name := range c.wPool.workersList() {
runCount++
c.launchWorker(name)
}
if runCount == 0 {
cancel()
return errors.New("unable to start: there is no initialized workers")
}
c.rtWorkersWG.Add(1)
go func() {
defer c.rtWorkersWG.Done()
c.broker.Serve(ctx)
}()
if c.sw != nil {
c.rtWorkersWG.Add(1)
go func() {
defer c.rtWorkersWG.Done()
if err := c.sw.Serve(ctx); err != nil {
c.eventChan <- ErrorEvent(
fmt.Sprintf("failed to run listener: %s", err)).
SetWorker("internal_socket_listener")
}
}()
}
<-c.ctx.Done()
cancel()
c.rtWorkersWG.Wait()
return nil
}
func (c *chief) launchWorker(name WorkerName) {
c.rtWorkersWG.Add(1)
mailbox := c.broker.AddWorker(name)
go c.runWorker(NewContext(c.rtWorkersCtx, mailbox), name, c.rtWorkersWG.Done)
}
func (c *chief) runWorker(ctx Context, name WorkerName, doneCall func()) {
defer doneCall()
err := c.wPool.runWorkerExec(ctx, c.eventChan, name)
if err != nil {
c.eventChan <- ErrorEvent(err.Error()).SetWorker(name)
}
}
func waitForSignal() {
gracefulStop := make(chan os.Signal, 1)
signal.Notify(gracefulStop, syscall.SIGTERM, syscall.SIGINT)
<-gracefulStop
}