-
Notifications
You must be signed in to change notification settings - Fork 373
/
main.go
62 lines (49 loc) · 1.22 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
package main
import (
"context"
"os/signal"
"sync"
"syscall"
"time"
"github.com/sirupsen/logrus"
"github.com/supabase/auth/cmd"
"github.com/supabase/auth/internal/observability"
)
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 profiler, 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
}
}