Gracefully shutdown in HTTP and gRPC apps

This commit is contained in:
Lam Tran
2022-01-06 22:57:05 +07:00
parent 6a032cb080
commit f989bd86b5
6 changed files with 39 additions and 11 deletions

View File

@@ -52,20 +52,19 @@ func HandleRESTRequests(wg *sync.WaitGroup) {
}
}()
logger.Log(0, "REST Server successfully started on port ", port, " (REST)")
c := make(chan os.Signal)
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals
signal.Notify(c, os.Interrupt)
ctx, stop := signal.NotifyContext(context.TODO(), os.Interrupt)
defer stop()
// Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-c
<-ctx.Done()
// After receiving CTRL+C Properly stop the server
logger.Log(0, "Stopping the REST server...")
srv.Shutdown(context.TODO())
logger.Log(0, "REST Server closed.")
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
}