Karta.Messaging.Attempts (Karta v1.0.0)

The life of one outbound message (an attempt of a send request on one connector): making it, handing it to the plug or the agent, and moving its state when the plug or the agent reports.

States only go forward. queued, waiting, sending, sent, delivered, read: a later state never goes back to an earlier one (a delivered that arrives before the sent it follows is kept). failed is accepted unless the message was delivered or read, unknown only before it was sent. An event that was applied before (the same source_event_id) changes nothing, so a redelivered event is harmless. Each accepted change is a row of the message's history (Karta.Messaging.MessageEvent), and a webhook event when it is a sent, delivered, read, failed or unknown (the channel used, the cost where known and whether the message was a fallback are always in it).

After a change that ends an attempt badly (failed, unknown) or well, the router decides what happens to the request (Karta.Messaging.Router.attempt_changed/1).

Summary

Functions

Makes the attempt attempt_no of the request on the connector, and hands it over: a command in the connector's outbox (remote plug) or a job (Sender). opts: fallback_from (the channel before this one), duplicate_possible, ttl (seconds the command stays valid). Returns the message. Call it outside a transaction.

The attempt failed: class, optional code, message, retry_after_s, at.

The agent answered a command that had already expired: the message was sent after all. It is not moved (it was given up, and the request may have tried again); every attempt of its request is flagged duplicate_possible and the history says so.

Notes a temporary non-delivery the provider reported (sms.undelivered) in the message's history without moving it: the carrier may still deliver. source_event_id makes it apply once. Returns :noted or :duplicate.

The attempt was handed to the agent or the plug (sending).

The attempt was accepted: provider_id, at, optional cost, command_id.

Moves the message to target if that is a step forward. opts: at, source_event_id (an event applied once), provider_id, cost, class, code, message. Returns {:changed, message}, :duplicate (the event was applied before), :ignored (not a step forward) or :not_found; a change tells the router.

Nobody can tell whether the message was sent.

Functions

create(request, connector, attempt_no, opts \\ [])

Makes the attempt attempt_no of the request on the connector, and hands it over: a command in the connector's outbox (remote plug) or a job (Sender). opts: fallback_from (the channel before this one), duplicate_possible, ttl (seconds the command stays valid). Returns the message. Call it outside a transaction.

failed(message_id, opts)

@spec failed(Ecto.UUID.t(), map()) :: term()

The attempt failed: class, optional code, message, retry_after_s, at.

late_sent(message_id, provider_id, at)

@spec late_sent(Ecto.UUID.t(), String.t() | nil, DateTime.t()) :: :ok

The agent answered a command that had already expired: the message was sent after all. It is not moved (it was given up, and the request may have tried again); every attempt of its request is flagged duplicate_possible and the history says so.

note(message_id, state, opts)

@spec note(Ecto.UUID.t(), String.t(), map()) :: :noted | :duplicate

Notes a temporary non-delivery the provider reported (sms.undelivered) in the message's history without moving it: the carrier may still deliver. source_event_id makes it apply once. Returns :noted or :duplicate.

sending(message_id)

@spec sending(Ecto.UUID.t()) :: :ok

The attempt was handed to the agent or the plug (sending).

sent(message_id, opts)

@spec sent(Ecto.UUID.t(), map()) :: term()

The attempt was accepted: provider_id, at, optional cost, command_id.

transition(message_id, target, opts)

@spec transition(Ecto.UUID.t(), atom(), map()) ::
  {:changed, Karta.Messaging.Message.t()} | :duplicate | :ignored | :not_found

Moves the message to target if that is a step forward. opts: at, source_event_id (an event applied once), provider_id, cost, class, code, message. Returns {:changed, message}, :duplicate (the event was applied before), :ignored (not a step forward) or :not_found; a change tells the router.

unknown(message_id, opts)

@spec unknown(Ecto.UUID.t(), map()) :: term()

Nobody can tell whether the message was sent.