From 7b3ebcc9cb2676d921aa1fcdef84733eb9e6c819 Mon Sep 17 00:00:00 2001 From: Adnan Hajdarevic Date: Thu, 15 Jan 2015 16:49:32 +0100 Subject: [PATCH] added signal watcher --- webhook.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/webhook.go b/webhook.go index 0a2b5cc..39de902 100644 --- a/webhook.go +++ b/webhook.go @@ -6,7 +6,10 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "os/exec" + "os/signal" + "syscall" "time" "github.com/adnanh/webhook/hooks" @@ -26,6 +29,7 @@ const ( var ( webhooks *hooks.Hooks appStart time.Time + signalChannel chan<- os.Signal ip = flag.String("ip", "", "ip the webhook server should listen on") port = flag.Int("port", 9000, "port the webhook server should listen on") hooksFilename = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve") @@ -41,6 +45,18 @@ func init() { martini.Env = "production" l4g.AddFilter("file", l4g.FINE, fileLogWriter) + + signalChannel := make(chan os.Signal, 2) + + signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM) + go func() { + sig := <-signalChannel + switch sig { + case syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGABRT: + l4g.Info("Caught kill signal, stopping webhook.", sig) + l4g.Close() + } + }() } func main() {