# `Juggernaut.Peer.Health`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/peer/health.ex#L1)

The health check a worker runs after every job (design notes §10.5, plan I18): a
fingerprint of the VM's shared state taken after boot, compared with one taken after
the job. Any difference is drift, and the worker is recycled after it reports the
job's result, so one job's leftovers never reach the next.

The fingerprint holds:

  * registered process names;
  * named ETS tables, and the number of ETS tables;
  * the Logger level and handler ids;
  * Ecto SQL sandbox owner processes left alive (labelled `{:sql_sandbox_owner, _}`);
  * the process and port counts;
  * modules loaded from memory (no file on disk), as tests define them;
  * the supervision trees of the project's applications: each child with an id and
    its pid (a new pid is a restart), and the child count of supervisors whose children
    have no id (`DynamicSupervisor`).

Lasting supervisors (the `lasting_supervisors` setting, `:lasting`) hold state that
tests are meant to leave behind: a connection pool started on first use (Req's
`Req.FinchSupervisor`), sessions a test starts and the app keeps. Everything in their
subtrees (the processes, including those that name a subtree process among their
`$ancestors`, their registered names, the ETS tables and ports they own, their
children) is left out of the fingerprint, so it is never drift; the lasting supervisor
itself is still watched (a restart is drift). `lasting_residue/2` reports a change in
such a subtree instead, so that a survivor found on a worker holding it runs again on
a fresh one.

`check/3` compares again a few times, a little apart, before it reports drift: processes
a test left can take a moment to exit after `ExUnit.run/1` returns. When there is
drift, its last reason names the supervisors (registered at boot) the new state was
created under, the candidates for `lasting_supervisors`.

This module runs inside workers and is never mutated.

# `t`

```elixir
@type t() :: %{
  registered: MapSet.t(atom()),
  named_tables: MapSet.t(atom()),
  tables: non_neg_integer(),
  logger: {atom(), [atom()]},
  sandbox_owners: non_neg_integer(),
  processes: non_neg_integer(),
  pids: MapSet.t(pid()),
  ports: non_neg_integer(),
  memory_modules: MapSet.t(module()),
  supervisors: %{required([term()]) =&gt; pid() | {:children, non_neg_integer()}}
}
```

# `check`

```elixir
@spec check(t(), [atom()], keyword()) :: [String.t()]
```

Compares the fingerprint of now with `boot` until they agree, up to `attempts` times
`pause_ms` apart, leaving out the `lasting` supervisors' subtrees; returns the drift
reasons of the last comparison (`[]` when healthy), followed by where the new state
lives when that can be told.

# `describe`

```elixir
@spec describe(pid()) :: String.t()
```

How the process `pid` started: its registered name, initial call (the one `proc_lib`
records, when there is one), current function and ancestors.

# `drift`

```elixir
@spec drift(t(), t()) :: [String.t()]
```

The differences between two fingerprints, as readable reasons (`[]` when none).

# `fingerprint`

```elixir
@spec fingerprint([atom()], [atom()]) :: t()
```

The fingerprint of the VM, with the supervision trees of `apps`, leaving out the
subtrees of the `lasting` supervisors (registered names).

# `lasting_counts`

```elixir
@spec lasting_counts([atom()]) :: %{required(atom()) =&gt; pos_integer()}
```

The number of processes in each running `lasting` supervisor's subtree (the
supervisor included), by name; one that is not running is left out.

# `lasting_residue`

```elixir
@spec lasting_residue(%{required(atom()) =&gt; pos_integer()}, [atom()]) :: [String.t()]
```

What changed in the `lasting` supervisors' subtrees since `boot` (`lasting_counts/1`),
as readable reasons (`[]` when nothing).

---

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