Ingot.GracefulShutdown (Ingot v0.1.0)

View Source

Drains LiveView connections before shutdown. Called by Kubernetes preStop hook.

This module ensures graceful shutdown of the Phoenix endpoint by:

  1. Broadcasting a shutdown message to all connected LiveView clients
  2. Waiting for active sessions to complete (up to 30 seconds)
  3. Allowing Kubernetes to terminate the pod cleanly

Usage

In Kubernetes deployment.yaml:

lifecycle:
  preStop:
    exec:
      command: ["/app/bin/ingot", "rpc", "Ingot.GracefulShutdown.drain()"]

Summary

Functions

Returns the count of active LiveView sessions.

Initiates graceful shutdown by broadcasting shutdown message and waiting for connections to drain.

Functions

count_active_sessions()

@spec count_active_sessions() :: non_neg_integer()

Returns the count of active LiveView sessions.

This function counts the number of active LiveView processes. In the current implementation, this returns 0 as LiveView connections are ephemeral and will naturally drain when the endpoint stops accepting new connections.

A production implementation could track active sessions via:

  • Phoenix.Tracker for distributed session tracking
  • Custom Registry for local process counting
  • Endpoint telemetry events for connection metrics

Examples

iex> Ingot.GracefulShutdown.count_active_sessions()
0

drain()

@spec drain() :: :ok

Initiates graceful shutdown by broadcasting shutdown message and waiting for connections to drain.

Returns :ok after all connections have drained or the timeout is reached.

Examples

iex> Ingot.GracefulShutdown.drain()
:ok