cmd/server: improve cleanup on shutdown
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
		| @ -21,18 +21,19 @@ var running = true | ||||
| func main() { | ||||
| 	log.SetLevel(log.DebugLevel) | ||||
| 	log.SetFormatter(&log.JSONFormatter{}) | ||||
| 	l := log.WithField("logger", "authentik.root") | ||||
| 	config.DefaultConfig() | ||||
| 	err := config.LoadConfig("./authentik/lib/default.yml") | ||||
| 	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") | ||||
| 	if err != nil { | ||||
| 		log.WithError(err).Debug("no local config to load") | ||||
| 		l.WithError(err).Debug("no local config to load") | ||||
| 	} | ||||
| 	err = config.FromEnv() | ||||
| 	if err != nil { | ||||
| 		log.WithError(err).Debug("failed to environment variables") | ||||
| 		l.WithError(err).Debug("failed to environment variables") | ||||
| 	} | ||||
| 	config.ConfigureLogger() | ||||
|  | ||||
| @ -45,7 +46,7 @@ func main() { | ||||
| 			Environment:      config.G.ErrorReporting.Environment, | ||||
| 		}) | ||||
| 		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) | ||||
| 		} | ||||
| 	} | ||||
| 	defer g.Kill() | ||||
| 	defer ws.Shutdown() | ||||
| 	go web.RunMetricsServer() | ||||
| 	for { | ||||
| 		go attemptStartBackend(g) | ||||
| @ -70,19 +69,19 @@ func main() { | ||||
|  | ||||
| 		<-ex | ||||
| 		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() | ||||
| 		log.WithField("logger", "authentik").Info("killing gunicorn") | ||||
| 		g.Kill() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func attemptStartBackend(g *gounicorn.GoUnicorn) { | ||||
| 	for { | ||||
| 		err := g.Start() | ||||
| 		if !running { | ||||
| 			return | ||||
| 		} | ||||
| 		err := g.Start() | ||||
| 		log.WithField("logger", "authentik.router").WithError(err).Warning("gunicorn process died, restarting") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -4,6 +4,8 @@ import ( | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"runtime" | ||||
| 	"syscall" | ||||
| 	"time" | ||||
|  | ||||
| 	log "github.com/sirupsen/logrus" | ||||
| @ -49,7 +51,7 @@ func (g *GoUnicorn) IsRunning() bool { | ||||
|  | ||||
| func (g *GoUnicorn) Start() error { | ||||
| 	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 | ||||
| 	} | ||||
| 	if g.started { | ||||
| @ -91,8 +93,15 @@ func (g *GoUnicorn) healthcheck() { | ||||
|  | ||||
| func (g *GoUnicorn) Kill() { | ||||
| 	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 { | ||||
| 		g.log.WithError(err).Warning("failed to kill gunicorn") | ||||
| 		g.log.WithError(err).Warning("failed to stop gunicorn") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -26,7 +26,7 @@ func (ws *WebServer) listenTLS() { | ||||
| 		ws.log.WithError(err).Fatalf("failed to listen") | ||||
| 		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)}} | ||||
| 	defer proxyListener.Close() | ||||
|  | ||||
| @ -74,14 +74,13 @@ func (ws *WebServer) listenPlain() { | ||||
| 	if err != nil { | ||||
| 		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} | ||||
| 	defer proxyListener.Close() | ||||
|  | ||||
| 	ws.serve(proxyListener) | ||||
|  | ||||
| 	ws.log.WithField("addr", config.G.Web.Listen).Info("Running") | ||||
| 	err = http.ListenAndServe(config.G.Web.Listen, ws.m) | ||||
| 	if err != nil && !errors.Is(err, http.ErrServerClosed) { | ||||
| 		ws.log.Errorf("ERROR: http.Serve() - %s", err) | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer