-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (43 loc) · 977 Bytes
/
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
package main
import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/ihcsim/kvm-device-plugin/pkg/plugins/kvm"
"github.com/rs/zerolog"
)
func main() {
var (
log = logger()
plugin = kvm.NewPlugin(log)
)
defer func() {
if err := plugin.Cleanup(); err != nil {
log.Error().Err(err).Msg("cleanup failed")
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
log.Info().Msg("received signal, attempting graceful shutdown")
cancel()
}()
if err := plugin.Run(ctx); err != nil {
if !errors.Is(ctx.Err(), context.Canceled) {
log.Error().Err(err).Send()
return
}
}
log.Info().Msg("shutdown completed successfully")
}
func logger() *zerolog.Logger {
w := zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}
l := zerolog.New(w).With().Timestamp().Logger()
return &l
}