# `Countersign.Sweep`
[🔗](https://github.com/mkreyman/countersign/blob/v0.1.0/lib/countersign/sweep.ex#L1)

The daily sweep over pending requests (`Countersign.Workers.Daily` runs
it): one reminder for each request sent long enough ago, then expiry for
each request past its `expires_at`.

Both walk one scope at a time, in pages of 500 rows along
`signing_requests_due_index`, and move each row with a conditional update
that only matches a request that is still pending. A request signed,
revoked or reminded meanwhile is left as it is, so two sweeps running at
once never remind or expire one request twice.

# `expire`

```elixir
@spec expire(Countersign.Config.t(), DateTime.t()) :: non_neg_integer()
```

Marks every pending request whose `expires_at` is at or before `now`
expired, clears its token, and reports `:expire` to
`c:Countersign.Host.audit/3` and `c:Countersign.Host.notify/2`. Expiry runs
whatever `c:Countersign.Host.enabled?/1` answers: a link switched off must
still stop working on its date. Answers how many expired.

# `remind`

```elixir
@spec remind(Countersign.Config.t(), DateTime.t(), (Countersign.Request.t() -&gt; :ok)) ::
  non_neg_integer()
```

Sends one reminder to every pending request whose `reminded_at` is nil and
whose `last_sent_at` is at least `c:Countersign.Host.reminder_days/1` days
before `now`. A scope whose reminder days are `0`, or whose
`c:Countersign.Host.enabled?/1` answers false, gets none, and its requests
keep `reminded_at` nil so a later sweep reminds them once it is back on.

For each request reminded, `reminded_at` is set and `enqueue` is called
with the request in the same transaction, so a reminder whose delivery job
could not be written is not marked sent. `enqueue` answers `:ok`; anything
else rolls the row back and raises. Answers how many were reminded.

---

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