api: handle SIGINT gracefully

This commit is contained in:
Adhityaa 2018-06-03 14:03:24 +05:30
parent a44e389b10
commit 72ab137f06
2 changed files with 26 additions and 0 deletions

View File

@ -8,6 +8,7 @@ func main() {
exitIfError(smtpConfigure()) exitIfError(smtpConfigure())
exitIfError(oauthConfigure()) exitIfError(oauthConfigure())
exitIfError(createMarkdownRenderer()) exitIfError(createMarkdownRenderer())
exitIfError(setupSigintCleanup())
exitIfError(serveRoutes()) exitIfError(serveRoutes())
} }

25
api/sigint.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"os"
"os/signal"
"syscall"
)
func sigintCleanup() int {
// TODO: close the database connection and do other cleanup jobs
return 0
}
func setupSigintCleanup() error {
logger.Infof("setting up SIGINT cleanup")
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGINT)
go func() {
<-c
os.Exit(sigintCleanup())
}()
return nil
}