Skip to content

Commit

Permalink
Merge pull request runatlantis#215 from hootsuite/safe-shutdown
Browse files Browse the repository at this point in the history
Shut down server safely on SIGINT/TERM. Fixes runatlantis#21
  • Loading branch information
lkysow authored Dec 17, 2017
2 parents 03481b5 + 76e0de9 commit 0109bcf
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
package server

import (
"context"
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -236,8 +240,28 @@ func (s *Server) Start() error {
StackSize: 1024 * 8,
}, NewRequestLogger(s.Logger))
n.UseHandler(s.Router)
s.Logger.Warn("Atlantis started - listening on port %v", s.Port)
return cli.NewExitError(http.ListenAndServe(fmt.Sprintf(":%d", s.Port), n), 1)

// Ensure server gracefully drains connections when stopped.
stop := make(chan os.Signal, 1)
// Stop on SIGINTs and SIGTERMs.
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

server := &http.Server{Addr: fmt.Sprintf(":%d", s.Port), Handler: n}
go func() {
s.Logger.Warn("Atlantis started - listening on port %v", s.Port)
if err := server.ListenAndServe(); err != nil {
// When shutdown safely, there will be no error.
s.Logger.Err(err.Error())
}
}()
<-stop

s.Logger.Warn("Received interrupt. Safely shutting down")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) // nolint: vet
if err := server.Shutdown(ctx); err != nil {
return cli.NewExitError(fmt.Sprintf("while shutting down: %s", err), 1)
}
return nil
}

// Index is the / route.
Expand Down

0 comments on commit 0109bcf

Please sign in to comment.