forked from supabase/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (56 loc) · 1.36 KB
/
main.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
package main
import (
"context"
"os/signal"
"sync"
"syscall"
"time"
"github.com/netlify/gotrue/api"
"github.com/netlify/gotrue/cmd"
"github.com/netlify/gotrue/observability"
"github.com/sirupsen/logrus"
)
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
func main() {
execCtx, execCancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT)
defer execCancel()
go func() {
<-execCtx.Done()
logrus.Info("received graceful shutdown signal")
}()
// command is expected to obey the cancellation signal on execCtx and
// block while it is running
if err := cmd.RootCommand().ExecuteContext(execCtx); err != nil {
logrus.WithError(err).Fatal(err)
}
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), time.Minute)
defer shutdownCancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
// wait for API servers to shut down gracefully
api.WaitForCleanup(shutdownCtx)
}()
wg.Add(1)
go func() {
defer wg.Done()
// wait for metrics and trace exporters to shut down gracefully
observability.WaitForCleanup(shutdownCtx)
}()
cleanupDone := make(chan struct{})
go func() {
defer close(cleanupDone)
wg.Wait()
}()
select {
case <-shutdownCtx.Done():
// cleanup timed out
return
case <-cleanupDone:
// cleanup finished before timing out
return
}
}