withPausedRequestHandling<T> method

Future<T> withPausedRequestHandling<T>(
  1. Future<T> action()
)

Stops accepting new requests on the user-facing servers (the API server and, if enabled, the web server), waits for in-flight requests to settle, runs action, then resumes request handling.

Used during runtime operations that require the pod to be quiescent - e.g. applying migrations through InsightsEndpoint.applyMigrations - to provide the same safety guarantees as a pod restart.

The operator-facing Insights server is not paused, so the call that invoked action can complete and return its result.

Implementation

Future<T> withPausedRequestHandling<T>(
  Future<T> Function() action,
) async {
  await server.shutdown();
  await _webServer?.stop();
  final T result;
  try {
    result = await action();
  } catch (_) {
    // The action's error is the one to report.
    await _resumeRequestHandling();
    rethrow;
  }
  if (!await _resumeRequestHandling()) {
    throw StateError(
      'Failed to resume the Serverpod servers, see logs for details.',
    );
  }
  return result;
}