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)