-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil.go
More file actions
37 lines (30 loc) · 806 Bytes
/
util.go
File metadata and controls
37 lines (30 loc) · 806 Bytes
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
package cmd
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"github.com/sirupsen/logrus"
)
// FatalIfErr adds fatal level log and exit program if the given err is not nil.
func FatalIfErr(err error, msg string, fields ...logrus.Fields) {
if err == nil {
return
}
if len(fields) > 0 {
logrus.WithError(err).WithFields(fields[0]).Fatal(msg)
} else {
logrus.WithError(err).Fatal(msg)
}
}
// GracefulShutdown allows to clean up and shutdown program when termination signal captured.
func GracefulShutdown(wg *sync.WaitGroup, cancel context.CancelFunc) {
termCh := make(chan os.Signal, 1)
signal.Notify(termCh, syscall.SIGTERM, syscall.SIGINT)
<-termCh
logrus.Info("SIGTERM/SIGINT received, start to shutdown program")
cancel()
wg.Wait()
logrus.Info("Shutdown gracefully")
}