How to gracefully shut down (SIGTERM) an Apollo Server instance?
Asked Answered
E

1

14

The actual HTTP server instance can be killed with server.close(callback), but I'm not sure what will happen with any pending WebSocket operations (mutations or queries being run through WebSockets). Since http.Server doesn't really know anything about the WebSocket operations, it probably ignores them. How to properly make sure that when SIGTERM is received, the server stops accepting new requests/webSocket connections, finishes the pending ones and then closes?

Couldn't really find anything about this with Google.

Escort answered 11/3, 2020 at 16:6 Comment(0)
M
1

Assuming based on the year this question was asked that you are asking about ApolloServer v2. Apollo Server instance does provide stop function which as per documentation waits for all the background tasks running. So it will wait for existing queries to complete before terminating the server. The method is available for v3 as well.

ApolloServer.stop() is an async method that tells all of Apollo Server's background tasks to complete. It calls and awaits all serverWillStop plugin handlers (including the usage reporting plugin's handler, which sends a final usage report to Apollo Studio). This method takes no arguments.

If your server is a federated gateway, stop also stops gateway-specific background activities, such as polling for updated service configuration.

In some circumstances, Apollo Server calls stop automatically when the process receives a SIGINT or SIGTERM signal. See the stopOnTerminationSignals constructor option for details.

If you're using the apollo-server package (which handles setting up an HTTP server for you), this method first stops the HTTP server. Specifically, it:

  • Stops listening for new connections
  • Closes idle connections (i.e., connections with no current HTTP request)
  • Closes active connections whenever they become idle
  • Waits for all connections to be closed

If any connections remain active after a grace period (10 seconds by default), Apollo Server forcefully closes those connections. You can configure this grace period with the stopGracePeriodMillis constructor option.

If you're using a middleware package instead of apollo-server, you should stop your HTTP server before calling ApolloServer.stop().

Motorboat answered 14/9, 2022 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.