From 05161db4588b1d72133fff6ec93c6285f7cef5aa Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 17 Jul 2021 18:04:09 +0200 Subject: [PATCH] cmd: fix shutdown not being signaled properl Signed-off-by: Jens Langhammer --- cmd/server/main.go | 27 ++++++++++++--------------- internal/gounicorn/gounicorn.go | 2 +- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index e7db0fb2c..04cbc63d5 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "net/url" - "os" "time" "github.com/getsentry/sentry-go" @@ -17,6 +16,8 @@ import ( "goauthentik.io/internal/web" ) +var running = true + func main() { log.SetLevel(log.DebugLevel) config.DefaultConfig() @@ -44,31 +45,30 @@ func main() { defer g.Kill() defer ws.Shutdown() for { - go attemptStartBackend(g, ex) + go attemptStartBackend(g) ws.Start() - // go attemptProxyStart(u, ex) + // go attemptProxyStart(u) <-ex - log.WithField("logger", "authentik").Debug("shutting down webserver") + running = false + log.WithField("logger", "authentik").Info("shutting down webserver") go ws.Shutdown() - log.WithField("logger", "authentik").Debug("killing gunicorn") + log.WithField("logger", "authentik").Info("killing gunicorn") g.Kill() } } -func attemptStartBackend(g *gounicorn.GoUnicorn, exitSignal chan os.Signal) error { +func attemptStartBackend(g *gounicorn.GoUnicorn) error { for { err := g.Start() - select { - case <-exitSignal: + if !running { return nil - default: - log.WithField("logger", "authentik.g").WithError(err).Warning("gunicorn process died, restarting") } + log.WithField("logger", "authentik.g").WithError(err).Warning("gunicorn process died, restarting") } } -func attemptProxyStart(u *url.URL, exitSignal chan os.Signal) error { +func attemptProxyStart(u *url.URL) error { maxTries := 100 attempt := 0 for { @@ -93,12 +93,9 @@ func attemptProxyStart(u *url.URL, exitSignal chan os.Signal) error { } continue } - select { - case <-exitSignal: + if !running { ac.Shutdown() return nil - default: - break } } return nil diff --git a/internal/gounicorn/gounicorn.go b/internal/gounicorn/gounicorn.go index c08071253..55ff4e487 100644 --- a/internal/gounicorn/gounicorn.go +++ b/internal/gounicorn/gounicorn.go @@ -48,7 +48,7 @@ func (g *GoUnicorn) initCmd() { func (g *GoUnicorn) Start() error { if g.killed { - g.log.Info("Not restarting gunicorn since we're killed") + g.log.Debug("Not restarting gunicorn since we're killed") return nil } if g.started {