refactor(cli): server shutdown (#3608)

* refactor(cli): add shutdownHTTPServer helper
* nit: reword comment to clarify

---------

Authored-by: Aaron Alpar <aaron.alpar@veeam.com>
This commit is contained in:
Julio López
2024-02-06 21:57:31 -08:00
committed by GitHub
parent ed9d0c281e
commit 4bec8e9bf8

View File

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