refactor: handle SIGTERM

This commit is contained in:
Cyrille Nofficial 2022-02-15 18:18:23 +01:00
parent 9b5e9c2320
commit 63af73a5f4

View File

@ -6,6 +6,8 @@ import (
"github.com/cyrilix/robocar-base/cli" "github.com/cyrilix/robocar-base/cli"
"go.uber.org/zap" "go.uber.org/zap"
"log" "log"
"os/signal"
"syscall"
"os" "os"
) )
@ -70,9 +72,7 @@ func main() {
log.Fatalf("unable to init logger: %v", err) log.Fatalf("unable to init logger: %v", err)
} }
defer func() { defer func() {
if err := lgr.Sync(); err != nil { _ = lgr.Sync()
log.Printf("unable to Sync logger: %v\n", err)
}
}() }()
zap.ReplaceGlobals(lgr) zap.ReplaceGlobals(lgr)
@ -80,12 +80,24 @@ func main() {
if err != nil { if err != nil {
zap.S().Fatalf("unable to connect to mqtt broker: %v", err) zap.S().Fatalf("unable to connect to mqtt broker: %v", err)
} }
defer client.Disconnect(10) defer func() {
zap.S().Infof("disconnect mqtt connection")
client.Disconnect(10)
}()
sc := arduino.NewAsymetricPWMSteeringConfig(steeringLeftPWM, steeringRightPWM, steeringCenterPWM) sc := arduino.NewAsymetricPWMSteeringConfig(steeringLeftPWM, steeringRightPWM, steeringCenterPWM)
a := arduino.NewPart(client, device, baud, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic, pubFrequency, sc) a := arduino.NewPart(client, device, baud, throttleTopic, steeringTopic, driveModeTopic, switchRecordTopic, pubFrequency, sc)
go func() {
err = a.Start() err = a.Start()
if err != nil { if err != nil {
zap.S().Errorw("unable to start service", "error", err) zap.S().Panicw("unable to start service", "error", err)
} }
}()
defer a.Stop()
exitChan := make(chan os.Signal)
signal.Notify(exitChan, syscall.SIGTERM)
<-exitChan
zap.S().Info("exit on sigterm")
} }