cmd/server: improve cleanup on shutdown

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-06 13:54:59 +01:00
parent 011babbbd9
commit 74382c6287
4 changed files with 23 additions and 16 deletions

View File

@ -21,18 +21,19 @@ var running = true
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.JSONFormatter{}) log.SetFormatter(&log.JSONFormatter{})
l := log.WithField("logger", "authentik.root")
config.DefaultConfig() config.DefaultConfig()
err := config.LoadConfig("./authentik/lib/default.yml") err := config.LoadConfig("./authentik/lib/default.yml")
if err != nil { if err != nil {
log.WithError(err).Warning("failed to load default config") l.WithError(err).Warning("failed to load default config")
} }
err = config.LoadConfig("./local.env.yml") err = config.LoadConfig("./local.env.yml")
if err != nil { if err != nil {
log.WithError(err).Debug("no local config to load") l.WithError(err).Debug("no local config to load")
} }
err = config.FromEnv() err = config.FromEnv()
if err != nil { if err != nil {
log.WithError(err).Debug("failed to environment variables") l.WithError(err).Debug("failed to environment variables")
} }
config.ConfigureLogger() config.ConfigureLogger()
@ -45,7 +46,7 @@ func main() {
Environment: config.G.ErrorReporting.Environment, Environment: config.G.ErrorReporting.Environment,
}) })
if err != nil { if err != nil {
log.WithError(err).Warning("failed to init sentry") l.WithError(err).Warning("failed to init sentry")
} }
} }
@ -61,8 +62,6 @@ func main() {
go attemptProxyStart(ws, u) go attemptProxyStart(ws, u)
} }
} }
defer g.Kill()
defer ws.Shutdown()
go web.RunMetricsServer() go web.RunMetricsServer()
for { for {
go attemptStartBackend(g) go attemptStartBackend(g)
@ -70,19 +69,19 @@ func main() {
<-ex <-ex
running = false running = false
log.WithField("logger", "authentik").Info("shutting down webserver") l.WithField("logger", "authentik").Info("shutting down gunicorn")
go g.Kill()
l.WithField("logger", "authentik").Info("shutting down webserver")
go ws.Shutdown() go ws.Shutdown()
log.WithField("logger", "authentik").Info("killing gunicorn")
g.Kill()
} }
} }
func attemptStartBackend(g *gounicorn.GoUnicorn) { func attemptStartBackend(g *gounicorn.GoUnicorn) {
for { for {
err := g.Start()
if !running { if !running {
return return
} }
err := g.Start()
log.WithField("logger", "authentik.router").WithError(err).Warning("gunicorn process died, restarting") log.WithField("logger", "authentik.router").WithError(err).Warning("gunicorn process died, restarting")
} }
} }

View File

@ -4,6 +4,8 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"runtime"
"syscall"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -49,7 +51,7 @@ func (g *GoUnicorn) IsRunning() bool {
func (g *GoUnicorn) Start() error { func (g *GoUnicorn) Start() error {
if g.killed { if g.killed {
g.log.Debug("Not restarting gunicorn since we're killed") g.log.Debug("Not restarting gunicorn since we're shutdown")
return nil return nil
} }
if g.started { if g.started {
@ -91,8 +93,15 @@ func (g *GoUnicorn) healthcheck() {
func (g *GoUnicorn) Kill() { func (g *GoUnicorn) Kill() {
g.killed = true g.killed = true
err := g.p.Process.Kill() var err error
if runtime.GOOS == "darwin" {
g.log.WithField("method", "kill").Warning("stopping gunicorn")
err = g.p.Process.Kill()
} else {
g.log.WithField("method", "sigterm").Warning("stopping gunicorn")
err = syscall.Kill(g.p.Process.Pid, syscall.SIGTERM)
}
if err != nil { if err != nil {
g.log.WithError(err).Warning("failed to kill gunicorn") g.log.WithError(err).Warning("failed to stop gunicorn")
} }
} }

View File

@ -26,7 +26,7 @@ func (ws *WebServer) listenTLS() {
ws.log.WithError(err).Fatalf("failed to listen") ws.log.WithError(err).Fatalf("failed to listen")
return return
} }
ws.log.WithField("addr", config.G.Web.ListenTLS).Info("Running") ws.log.WithField("addr", config.G.Web.ListenTLS).Info("Listening (TLS)")
proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}} proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}}
defer proxyListener.Close() defer proxyListener.Close()

View File

@ -74,14 +74,13 @@ func (ws *WebServer) listenPlain() {
if err != nil { if err != nil {
ws.log.WithError(err).Fatalf("failed to listen") ws.log.WithError(err).Fatalf("failed to listen")
} }
ws.log.WithField("addr", config.G.Web.Listen).Info("Running") ws.log.WithField("addr", config.G.Web.Listen).Info("Listening")
proxyListener := &proxyproto.Listener{Listener: ln} proxyListener := &proxyproto.Listener{Listener: ln}
defer proxyListener.Close() defer proxyListener.Close()
ws.serve(proxyListener) ws.serve(proxyListener)
ws.log.WithField("addr", config.G.Web.Listen).Info("Running")
err = http.ListenAndServe(config.G.Web.Listen, ws.m) err = http.ListenAndServe(config.G.Web.Listen, ws.m)
if err != nil && !errors.Is(err, http.ErrServerClosed) { if err != nil && !errors.Is(err, http.ErrServerClosed) {
ws.log.Errorf("ERROR: http.Serve() - %s", err) ws.log.Errorf("ERROR: http.Serve() - %s", err)