Skip to content

Commit

Permalink
feat(backend): graceful shutdown support
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed May 12, 2019
1 parent af4c1c6 commit 20e58f0
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"

"github.com/prymitive/karma/internal/alertmanager"
Expand Down Expand Up @@ -231,10 +235,28 @@ func main() {
log.Fatalf("Failed to setup proxy handlers for Alertmanager '%s': %s", am.Name, err)
}
}

listen := fmt.Sprintf("%s:%d", config.Config.Listen.Address, config.Config.Listen.Port)
log.Infof("Listening on %s", listen)
err := router.Run(listen)
if err != nil {
log.Fatal(err)
httpServer := &http.Server{
Addr: listen,
Handler: router,
}

go func() {
if err := httpServer.ListenAndServe(); err != nil {
log.Infof("Listening on %s", listen)
}
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Infof("Shutting down HTTP server")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Fatalf("Shutdown failed: %s", err)
}
log.Info("HTTP server shut down")
}

0 comments on commit 20e58f0

Please sign in to comment.