cmd: fix shutdown not being signaled properl

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-07-17 18:04:09 +02:00
parent 311ffa9f79
commit 05161db458
2 changed files with 13 additions and 16 deletions

View File

@ -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

View File

@ -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 {