-
Notifications
You must be signed in to change notification settings - Fork 0
/
shutdown.go
39 lines (33 loc) · 874 Bytes
/
shutdown.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
package main
import (
"context"
"sync"
"github.com/rs/zerolog/log"
)
type Shutdownable interface {
Shutdown(ctx context.Context) error
}
func gracefullyShutdown(ctx context.Context, name string, srv Shutdownable, wg *sync.WaitGroup) {
err := srv.Shutdown(ctx)
if err != nil {
log.Error().Err(err).Str("server", name).Msg("Server failed to shutdown")
} else {
log.Info().Str("server", name).Msg("Server shutdown complete")
}
wg.Done()
}
func gracefullyShutdownAll(servers map[string]Shutdownable) {
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
log.Info().Msg("Shutting down")
wg := &sync.WaitGroup{}
wg.Add(len(servers))
for name, server := range servers {
name, server := name, server
go func() {
gracefullyShutdown(ctx, name, server, wg)
}()
}
wg.Wait()
log.Info().Msg("Shutdown complete")
}