# `SuperPi.Drain`
[🔗](https://github.com/oleksandr/super-pi-beam/blob/v0.1.0/lib/super_pi/drain.ex#L1)

Graceful shutdown of a node (REQ-CLU-07).

On SIGTERM the application calls `drain/1` before it stops. The node

1. stops accepting new work: the gateway answers `503 shutting_down` (with `Retry-After`) and
   `GET /ready` returns 503, so a load balancer or Kubernetes sends traffic elsewhere; RPC
   refuses `session.open` and `session.submit`;
2. waits for the conversation machines on this node to finish their running turns, up to
   `config :super_pi, drain_timeout_ms` (default 30 000);
3. stops. Idle machines under `Horde.DynamicSupervisor` are restarted on the other nodes, and a
   turn that did not finish in time is resumed from the durable state by the machine that
   replaces it.

# `accepting?`

True while the node accepts new work.

# `busy_conversations`

Ids of the conversations on this node whose machine is in the middle of a turn.

# `drain`

Stops accepting work and waits until no local machine is busy. Returns `:drained` or
`{:timeout, busy_conversation_ids}`.

# `resume`

Accepts work again (used by tests, and after a cancelled shutdown).

# `stop_accepting`

Stops accepting new work.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
