Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): graceful shutdown support #713

Merged
merged 1 commit into from
May 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}