mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-08 14:15:25 +08:00
Gracefully shutdown in HTTP and gRPC apps
This commit is contained in:
parent
6a032cb080
commit
f989bd86b5
6 changed files with 39 additions and 11 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -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
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/netmaker.iml" filepath="$PROJECT_DIR$/.idea/netmaker.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
9
.idea/netmaker.iml
generated
Normal file
9
.idea/netmaker.iml
generated
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -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)))
|
||||
|
||||
}
|
||||
|
|
12
main.go
12
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.")
|
||||
|
|
Loading…
Add table
Reference in a new issue