Karta.Outbound (Karta v1.0.0)

Outbound delivery: queueing accepted outgoing messages, and the per-recipient state the delivery jobs keep.

An outgoing message is accepted and queued in one transaction (accept/2): the message moves from :pending to :accepted, each envelope recipient gets a Karta.Outbound.Recipient row (:queued), and each recipient domain gets one delivery job (Karta.Outbound.Deliverer) for that message, in the queue of the server's IP pool (Karta.IPPools.queue/1; outbound for a server without one). Either all of it is committed or none: an acknowledged message always has its jobs.

A recipient on the server's suppression list (Karta.Suppressions) gets a row with the status :suppressed and no delivery.

Accepting a message records the webhook events message.accepted and message.suppressed in the same transaction (Karta.Webhooks.emit/2).

Summary

Functions

Accepts the pending message message_id and queues its delivery to recipients ({address, rcpt_params} as the SMTP session collected them). {:error, :not_pending} when the message is not pending; nothing is written then.

The delivery attempts for a recipient or for a whole message, oldest first.

A recipient of an accepted message of this server, with its message, or nil. Anything that is not a UUID is simply not found.

The delivery job for the recipients of message_id at domain. opts are further Oban.Job.new/2 options (queue:, by default outbound; unique:, for example).

Inserts the recipient rows and the delivery jobs of an accepted message. Call it inside the transaction that accepts the message. Options as for accept/3.

The delivery queue of a message: its server's IP pool's (Karta.IPPools.queue/1).

How many deliveries the server recorded in the window seconds before now (default 60): the send limit counts these (Karta.Outbound.Deliverer).

The recipients of a message, oldest first.

Moves the waiting delivery jobs (available, scheduled or retryable) that are not in their server's queue there: those of one server, or of all with :all. Returns how many moved. This happens when a server's pool changed (Karta.IPPools.assign_server/2) or a job was queued while it changed.

How many recipients of the server's messages accepted since since are in each state: %{status => count} (states without recipients are left out).

How many recipients of each message are in each state: %{message_id => %{status => count}} (messages without recipients are left out).

Functions

accept(message_id, recipients, opts \\ [])

@spec accept(Ecto.UUID.t(), [{String.t(), map()}], keyword()) ::
  :ok | {:error, term()}

Accepts the pending message message_id and queues its delivery to recipients ({address, rcpt_params} as the SMTP session collected them). {:error, :not_pending} when the message is not pending; nothing is written then.

Options: suppressed - the recipient addresses (as given in recipients) not to deliver to; send_at - a DateTime before which nothing is delivered (the jobs are scheduled for it, and the recipients are not due until then); quota - true counts the message against its organisation's limits (Karta.Quotas), for mail a client submitted, and returns {:error, {:quota, reason}} when it would pass one.

deliveries(arg1)

The delivery attempts for a recipient or for a whole message, oldest first.

get_recipient(server, id)

@spec get_recipient(Karta.Servers.Server.t(), term()) ::
  Karta.Outbound.Recipient.t() | nil

A recipient of an accepted message of this server, with its message, or nil. Anything that is not a UUID is simply not found.

job(message_id, domain, opts \\ [])

@spec job(Ecto.UUID.t(), String.t(), keyword()) :: Ecto.Changeset.t()

The delivery job for the recipients of message_id at domain. opts are further Oban.Job.new/2 options (queue:, by default outbound; unique:, for example).

A job inserted one by one may be made unique among the unfinished jobs (unique: [period: :infinity, states: :incomplete]); Oban.insert_all/1 does not support uniqueness, and the queueing of an accepted message uses it. A second job is harmless either way: the recipients a job sends are claimed with one conditional update (Karta.Claim), so only one job sends them.

queue(message_id, recipients, opts \\ [])

@spec queue(Ecto.UUID.t(), [{String.t(), map()}], keyword()) :: :ok

Inserts the recipient rows and the delivery jobs of an accepted message. Call it inside the transaction that accepts the message. Options as for accept/3.

queue_of(message_id)

@spec queue_of(Ecto.UUID.t()) :: String.t()

The delivery queue of a message: its server's IP pool's (Karta.IPPools.queue/1).

recent_delivery_count(server, now, window \\ 60)

@spec recent_delivery_count(Karta.Servers.Server.t(), DateTime.t(), pos_integer()) ::
  non_neg_integer()

How many deliveries the server recorded in the window seconds before now (default 60): the send limit counts these (Karta.Outbound.Deliverer).

recipients(message_id)

@spec recipients(Ecto.UUID.t()) :: [Karta.Outbound.Recipient.t()]

The recipients of a message, oldest first.

reroute(server_id)

@spec reroute(Ecto.UUID.t() | :all) :: non_neg_integer()

Moves the waiting delivery jobs (available, scheduled or retryable) that are not in their server's queue there: those of one server, or of all with :all. Returns how many moved. This happens when a server's pool changed (Karta.IPPools.assign_server/2) or a job was queued while it changed.

server_status_counts(server, since)

@spec server_status_counts(Karta.Servers.Server.t(), DateTime.t()) :: %{
  required(atom()) => pos_integer()
}

How many recipients of the server's messages accepted since since are in each state: %{status => count} (states without recipients are left out).

status_counts(message_ids)

@spec status_counts([Ecto.UUID.t()]) :: %{
  required(Ecto.UUID.t()) => %{required(atom()) => pos_integer()}
}

How many recipients of each message are in each state: %{message_id => %{status => count}} (messages without recipients are left out).