From 72ab137f06371dca4875863d6c926850bd451fea Mon Sep 17 00:00:00 2001 From: Adhityaa Date: Sun, 3 Jun 2018 14:03:24 +0530 Subject: [PATCH] api: handle SIGINT gracefully --- api/main.go | 1 + api/sigint.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 api/sigint.go diff --git a/api/main.go b/api/main.go index a395a2c..43f8b60 100644 --- a/api/main.go +++ b/api/main.go @@ -8,6 +8,7 @@ func main() { exitIfError(smtpConfigure()) exitIfError(oauthConfigure()) exitIfError(createMarkdownRenderer()) + exitIfError(setupSigintCleanup()) exitIfError(serveRoutes()) } diff --git a/api/sigint.go b/api/sigint.go new file mode 100644 index 0000000..5b91edf --- /dev/null +++ b/api/sigint.go @@ -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 +}