From f989bd86b57d837a839985e165ddcf2d51e51154 Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Thu, 6 Jan 2022 22:57:05 +0700 Subject: [PATCH] Gracefully shutdown in HTTP and gRPC apps --- .idea/.gitignore | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/netmaker.iml | 9 +++++++++ .idea/vcs.xml | 6 ++++++ controllers/controller.go | 7 +++---- main.go | 12 +++++------- 6 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/netmaker.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..0ddbe09c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/netmaker.iml b/.idea/netmaker.iml new file mode 100644 index 00000000..5e764c4f --- /dev/null +++ b/.idea/netmaker.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/controllers/controller.go b/controllers/controller.go index 4129afa3..008d517f 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -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))) - } diff --git a/main.go b/main.go index c908c5bb..fd9aad97 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "net" "os" @@ -157,21 +158,18 @@ func runGRPC(wg *sync.WaitGroup) { }() logger.Log(0, "Agent Server successfully started on port ", grpcport, "(gRPC)") - // Right way to stop the server using a SHUTDOWN HOOK - // Create a channel to receive OS signals - c := make(chan os.Signal, 1) - // 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 Agent server...") - s.Stop() + s.GracefulStop() listener.Close() logger.Log(0, "Agent server closed..") logger.Log(0, "Closed DB connection.")