Karta. Changes
(Karta v1.0.0)
Announcements of changes that admin pages may show while they are open (see the Live updates guide).
The core announces a change after its transaction commits, on the
topic of the record it belongs to, with the kind of change and the ids
involved (Karta.Changes.Change), never content. A page that shows such
records subscribes to the topic of the record it loaded for its user
(subscribe/1) and, on an announcement, reads again through its own
scoped query: an announcement only says "look again".
Topics:
server:<id>- a message of the server accepted (:message_accepted), its recipients changed (:recipients), an open or a click recorded (:tracking), messages removed (:messages_removed), all with message ids; a received message's deliveries changed (:inbound_deliveries, with the messages' ids); its routes (:routes) or endpoints (:endpoints) changed, with their ids; its suppression list changed (:suppressions, with their ids); the server itself changed (:server, its id); its webhooks changed (:webhooks) or a delivery of their events (:webhook_deliveries), with their ids;message:<id>- the same kinds for one message (its id), its received deliveries included (:inbound_deliveries);organisation:<id>- its servers (:servers) or its domains (:domains) changed, with their ids;domain:<id>- the domain's verification or the check of its ownership record (:domain), its DNS health results (:dns_checks), its tracking host (:tracking_host) or its DKIM keys (:dkim_keys) changed, with its id.
broadcast/3 sends at once outside a transaction. Inside one it queues
the announcement in the process, and the transaction must be run by
transaction/2, which sends the queue after the outermost transaction
commits (one announcement per topic and kind, with all the ids) and drops
it when the transaction rolls back or raises. An announcement inside a
transaction that transaction/2 does not run would be sent before the
commit: with config :karta, Karta.Changes, strict: true (the tests) it
raises; otherwise it is sent at once and logged.
Karta.Relay carries announcements to the subscribers (Karta.PubSub)
on this node and, through the database, on every other replica.
Summary
Functions
Announces a change of kind to the records ids on the topic of
scope: at once outside a transaction, after the commit inside
transaction/2. Nothing is announced for no ids.
Subscribes the calling process to the changes of a record it has loaded
(never to an id taken from a request): it receives %Karta.Changes.Change{}
messages.
The topic of a record (%Server{}, %Message{}, %Organisation{}, %Domain{}) or of a scope/0.
Karta.Repo.transaction/2 (a function or an Ecto.Multi), with the
changes announced inside it sent after the outermost transaction
commits, and dropped when it rolls back or raises. Returns what
Repo.transaction/2 returns.
Types
@type announcing() :: Karta.Servers.Server.t() | Karta.Messages.Message.t() | Karta.Accounts.Organisation.t() | Karta.Domains.Domain.t()
A record whose changes are announced.
@type scope() :: {:server | :message | :organisation | :domain, Ecto.UUID.t()}
Where a change belongs: a record's kind and id.
Functions
@spec broadcast(scope(), atom(), [Ecto.UUID.t()]) :: :ok
Announces a change of kind to the records ids on the topic of
scope: at once outside a transaction, after the commit inside
transaction/2. Nothing is announced for no ids.
@spec subscribe(announcing()) :: :ok
Subscribes the calling process to the changes of a record it has loaded
(never to an id taken from a request): it receives %Karta.Changes.Change{}
messages.
@spec topic(announcing() | scope()) :: String.t()
The topic of a record (%Server{}, %Message{}, %Organisation{}, %Domain{}) or of a scope/0.
@spec transaction((-> any()) | Ecto.Multi.t(), keyword()) :: term()
Karta.Repo.transaction/2 (a function or an Ecto.Multi), with the
changes announced inside it sent after the outermost transaction
commits, and dropped when it rolls back or raises. Returns what
Repo.transaction/2 returns.