# `Ankusa.Queue.DlqReaper`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/queue/dlq_reaper.ex#L1)

Keeps the dead-letter queue bounded (`queue.dlq`), without ever discarding a
hook the provider was told `201` for.

A dead row is evicted when it is older than `queue.dlq.retention_days`, and
oldest first while the DLQ holds more than `queue.dlq.max_rows` rows or
`queue.dlq.max_bytes` stored bytes (the node's `dead` and `dead_bytes`
counters, one read), and **only while its hook is still recoverable**: no
archive obligation is left for it, and a segment the catalogue can find holds
its id (`Ankusa.Storage.fetch/2` answers after the hook is reclaimed), or it is
a hook a 0.3 node archived. A row that fails the test is skipped, never
deleted: the compactor drains the archive, the next pass evicts what it
archived, and that is what keeps the disk flat.

A DLQ that is still over a ceiling after a pass has nothing left to evict, so
the node cannot drain: `full?/1` turns `true` (the gauge `ankusa_dlq_full`, a
warning in the log) and the edge answers new hooks `503 dlq_full` with
`Retry-After: 60` until a pass finds the DLQ under its ceilings again. An
acked hook is never deleted to make room.

One process per instance, in the dispatch domain next to the Pipeline and the
Replayer. Every eviction runs in this process and each batch inside the
Replayer (`{:exclusive, fun}`), so a replay job cannot revive a row it is
deleting. A batch is at most `@chunk` rows of one tenant, written in one
synced batch with its audit row (`dlq.evict`, or `dlq.purge` for an operator's
`DELETE /v1/dlq`): the audit row carries the count, the bytes and the range of
seqs and ids, not one row per hook. Every batch emits `[:ankusa, :dlq,
:evicted]` (`count`, `bytes`; metadata `tenant`, `reason` and the `ids` and
`seqs` evicted).

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `evict`

```elixir
@spec evict(atom(), map(), pos_integer(), keyword()) ::
  {:ok,
   %{
     deleted: non_neg_integer(),
     bytes: non_neg_integer(),
     unarchived: non_neg_integer(),
     more: boolean()
   }}
  | {:error, term()}
```

Evict the dead rows matching `filter` (`:source_id`; `:tenants`, a list of
tenant ids or `nil`; `:since` / `:until`, inclusive bounds on the dead-letter
time, ms), at most `limit` of them, the oldest first, under the same rule as
the reaper: a row whose hook is not archived is skipped and counted in
`unarchived`, never deleted. `opts[:audit]` is the caller's
`Ankusa.Audit.Context`. `more` says matching rows may remain past `limit` or
the pass's scan budget.

# `full?`

```elixir
@spec full?(atom()) :: boolean()
```

Whether the DLQ is over a ceiling with nothing left that may be evicted: the
edge refuses new hooks while it is. A `:persistent_term` read, set by the
reaper only when the answer changes, so the request path pays nothing.

# `reap`

```elixir
@spec reap(atom()) ::
  {:ok, %{evicted: non_neg_integer(), full: boolean(), more: boolean()}}
  | {:error, term()}
```

One pass, synchronously: evict what retention and the ceilings allow.
`{:ok, %{evicted: n, full: boolean, more: boolean}}`; `more` says the pass's
budget, not the end of the DLQ, stopped it.

# `start_link`

---

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