From 4bec8e9bf82341c582afbcf4e321bc1b42a6a1f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20L=C3=B3pez?= <1953782+julio-lopez@users.noreply.github.com> Date: Tue, 6 Feb 2024 21:57:31 -0800 Subject: [PATCH] refactor(cli): server shutdown (#3608) * refactor(cli): add shutdownHTTPServer helper * nit: reword comment to clarify --------- Authored-by: Aaron Alpar --- cli/command_server_start.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cli/command_server_start.go b/cli/command_server_start.go index 2e7f1db00..4b08718ef 100644 --- a/cli/command_server_start.go +++ b/cli/command_server_start.go @@ -208,7 +208,7 @@ func (c *commandServerStart) run(ctx context.Context) error { ctx2, cancel := context.WithTimeout(ctx, c.shutdownGracePeriod) defer cancel() - // wait for all connections to finish for up to 5 seconds + // wait for all connections to finish within a shutdown grace period log(ctx2).Debugf("attempting graceful shutdown for %v", c.shutdownGracePeriod) if serr := httpServer.Shutdown(ctx2); serr != nil { @@ -223,11 +223,7 @@ func (c *commandServerStart) run(ctx context.Context) error { } c.svc.onTerminate(func() { - log(ctx).Infof("Shutting down...") - - if serr := httpServer.Shutdown(ctx); serr != nil { - log(ctx).Debugf("unable to shut down: %v", serr) - } + shutdownHTTPServer(ctx, httpServer) }) c.svc.onRepositoryFatalError(func(_ error) { @@ -258,8 +254,7 @@ func (c *commandServerStart) run(ctx context.Context) error { ctxutil.GoDetached(ctx, func(ctx context.Context) { // consume all stdin and close the server when it closes io.ReadAll(os.Stdin) //nolint:errcheck - log(ctx).Infof("Shutting down server...") - httpServer.Shutdown(ctx) //nolint:errcheck + shutdownHTTPServer(ctx, httpServer) }) } @@ -273,6 +268,14 @@ func (c *commandServerStart) run(ctx context.Context) error { return errors.Wrap(srv.SetRepository(ctx, nil), "error setting active repository") } +func shutdownHTTPServer(ctx context.Context, httpServer *http.Server) { + log(ctx).Infof("Shutting down HTTP server ...") + + if err := httpServer.Shutdown(ctx); err != nil { + log(ctx).Errorln("unable to shut down HTTP server:", err) + } +} + func (c *commandServerStart) setupHandlers(srv *server.Server, m *mux.Router) { if c.serverStartLegacyRepositoryAPI { srv.SetupRepositoryAPIHandlers(m)