Karta. Messaging. Commands
(Karta v1.0.0)
The outbox of a remote plug (Karta.Messaging.Command): what Karta wants
an agent to do. A command is a row, inserted in the transaction that
decides it, with a seq that is gap-free per connector (assigned while
the connector's row is locked). Whoever needs a command done only writes
it and announces it (Karta.Relay.messaging/3); the replica that holds
the agent's connection claims it (claim/2, one statement, so two
replicas never push the same command) and pushes it. Every connection
also claims every few seconds, because an announcement is best effort,
so the outbox works with KARTA_CLUSTER on or off and with any number
of replicas.
A command for a connector with no live connection waits until its
expires_at. One the agent never acknowledged then expires (sweep/1),
and its message fails with the class expired (nothing was sent). One
the agent acknowledged but never answered is not guessed at: the agent
answers it when it comes back, or it becomes unknown a week after.
Summary
Functions
The agent has the command in its journal.
Tells the replica that holds the connector's agent that a command waits.
Claims the connector's pending commands that have not expired, oldest
first, at most limit: they become sent. Returns them in seq order.
Writes a command for the connector. Options: ttl (seconds it stays
valid, default 300), message_id (the message it carries),
announce (default true; pass false inside a larger transaction and call
announce/1 after the commit).
Expires the commands of a connector the agent never acknowledged (revoked, deleted).
The agent's answer. The first answer completes the command; a repeated
one changes nothing. A send's answer moves its message
(Karta.Messaging.Attempts). Returns :ok, :unknown for a command the
connector does not have.
The frame body of a command at the moment it is pushed (a media link is made fresh).
True when the connector has an agent connected now (a session seen within grace seconds).
How many commands are with the agent and not answered.
The commands to push when an agent connects: those it was sent or
acknowledged but has not answered (it answers a repeated command_id
with the stored result, never executes it twice), then the pending ones.
Expires what can no longer be done: commands the agent never acknowledged
whose time has passed (their sends fail with the class expired), and
commands it acknowledged but never answered for a week (their sends
become unknown). Returns how many. Safe on several replicas.
Functions
@spec acknowledged(Karta.Messaging.Connector.t(), Ecto.UUID.t()) :: :ok
The agent has the command in its journal.
@spec announce(Ecto.UUID.t()) :: :ok
Tells the replica that holds the connector's agent that a command waits.
@spec claim(Ecto.UUID.t(), pos_integer()) :: [Karta.Messaging.Command.t()]
Claims the connector's pending commands that have not expired, oldest
first, at most limit: they become sent. Returns them in seq order.
@spec enqueue(Karta.Messaging.Connector.t(), String.t(), map(), keyword()) :: {:ok, Karta.Messaging.Command.t()}
Writes a command for the connector. Options: ttl (seconds it stays
valid, default 300), message_id (the message it carries),
announce (default true; pass false inside a larger transaction and call
announce/1 after the commit).
@spec expire_unacknowledged(Karta.Messaging.Connector.t()) :: :ok
Expires the commands of a connector the agent never acknowledged (revoked, deleted).
@spec finished(Karta.Messaging.Connector.t(), Ecto.UUID.t(), map()) :: :ok | :unknown
The agent's answer. The first answer completes the command; a repeated
one changes nothing. A send's answer moves its message
(Karta.Messaging.Attempts). Returns :ok, :unknown for a command the
connector does not have.
@spec frame( Karta.Messaging.Command.t(), Karta.Messaging.Connector.t(), String.t() | nil ) :: map()
The frame body of a command at the moment it is pushed (a media link is made fresh).
@spec live?(Karta.Messaging.Connector.t(), pos_integer()) :: boolean()
True when the connector has an agent connected now (a session seen within grace seconds).
@spec open_count(Ecto.UUID.t()) :: non_neg_integer()
How many commands are with the agent and not answered.
@spec resume(Ecto.UUID.t()) :: [Karta.Messaging.Command.t()]
The commands to push when an agent connects: those it was sent or
acknowledged but has not answered (it answers a repeated command_id
with the stored result, never executes it twice), then the pending ones.
@spec sweep(DateTime.t()) :: non_neg_integer()
Expires what can no longer be done: commands the agent never acknowledged
whose time has passed (their sends fail with the class expired), and
commands it acknowledged but never answered for a week (their sends
become unknown). Returns how many. Safe on several replicas.