Karta.Messages.Retention (Karta v1.0.0)

Removes stored messages that have outlived their server's retention settings, and cleans up after storage attempts that never finished.

One run (run/1):

  1. Unfinished - :pending rows older than :pending_grace (default 2 hours; an SMTP connection lives at most 1 hour) and :discarding rows, whose blob delete failed earlier.
  2. Expired - :accepted messages older than the server's retention_days.
  3. Over size - a server's oldest :accepted messages beyond retention_size_mb of stored bodies.

Kantoor policies (Karta.Kantoor.Retention, all optional): a message that carries none behaves exactly as described above. An outgoing message with a policy of its own follows it (keep: never removed by steps 2 and 3; days: removed by step 2 after that many days instead of the server's). An incoming message follows its deliveries' policies: it is removed by step 2 when every delivery is deleted or expired, a delivery without a policy expiring after the server's retention_days and one with keep never; so of several policies the longest wins and keep beats any number of days. A message with a live keep is exempt from step 3 and does not count towards the server's size.

A message with recipients still waiting for delivery (:queued or :deferred) is never removed by steps 2 and 3, however old or large: a queued message is not lost silently. It becomes removable once each recipient is delivered or bounced (at most 5 days later).

Each step works in batches (:batch_size, default 500; at most :max_batches per step and run, default 100) through Karta.Messages.discard/3, so a blob is deleted before its row and a failure only postpones the work to the next run. Runs on several nodes at once are safe: every transition is a conditional update.

As a process it runs every :interval milliseconds (configured under config :karta, Karta.Messages.Retention; nil disables it), the first time :first_run milliseconds after start.

Summary

Functions

Returns a specification to start this module under a supervisor.

Runs all three steps once. Options as in the module doc, plus :now (default the current time) and :blob_store (see Karta.Storage). Returns how many messages each step removed.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

run(opts \\ [])

@spec run(keyword()) :: %{
  unfinished: non_neg_integer(),
  expired: non_neg_integer(),
  over_size: non_neg_integer()
}

Runs all three steps once. Options as in the module doc, plus :now (default the current time) and :blob_store (see Karta.Storage). Returns how many messages each step removed.

start_link(opts \\ [])