AshHooks.OutboundEvent (AshHooks v3.0.0)

Copy Markdown View Source

Turns the consumer's resource into the outbound EVENT log: exactly one row per dispatched event — its id, type, declared payload version, time, and exact payload bytes — recorded whether or not any endpoint was enabled (or existed), with its own retention.

use Ash.Resource,
  data_layer: AshPostgres.DataLayer,
  extensions: [AshHooks.OutboundEvent]

# and on the emitting resource's declaration:
outbound :order_paid do
  events MyApp.OutboundEvent
end

The dispatcher writes through the injected :record machine action (an idempotent no-touch upsert on the event id); a log write failure never stops the fanout — it surfaces as [:ash_hooks, :event, :record_failed] telemetry. The delivery ledger remains the delivery contract; this log is the source of record for what was EMITTED — the answer behind "what did we send, ever, that endpoint X never received?".

Rows carry the exact payload bytes you dispatched — govern read access with your own policies. The resource needs timestamps() (the event's time is inserted_at), and your migration carries the table plus the unique_event index on [:event_uuid].

Policy obligation: protect the injected :record create and :prune destroy actions wherever your domain exposes the resource; the runtime's internal calls bypass authorization by design.

Summary

Functions

Lists logged events by filter with keyset pagination (newest first).

The event log's own retention hook: deletes logged events older than older_than, by inserted_at, in batches (:batch_size, default 500). Returns {:ok, deleted_count}.

Replays logged events to a CHOSEN endpoint — one event (:event_uuid) or a filtered set (:event_type with optional :from/:to) — including an endpoint that was disabled when the event happened or was added later. Replays KEEP the event id (a Standard Webhooks receiver deduplicates by it).

Functions

list(resource, opts \\ [])

@spec list(module(), keyword()) ::
  {:ok, %{records: [struct()], next_cursor: binary() | nil}} | {:error, term()}

Lists logged events by filter with keyset pagination (newest first).

Options: :event_type, :event_uuid, :from / :to (DateTime bounds on inserted_at, inclusive), :limit (default 50, max 500), :after (the previous page's cursor), and :tenant (the tenancy pre-flight applies). Returns {:ok, %{records:, next_cursor:}}.

prune(resource, opts)

@spec prune(module(), keyword()) :: {:ok, non_neg_integer()} | {:error, term()}

The event log's own retention hook: deletes logged events older than older_than, by inserted_at, in batches (:batch_size, default 500). Returns {:ok, deleted_count}.

replay(resource, endpoint, opts)

@spec replay(module(), struct(), keyword()) ::
  {:ok,
   %{
     created: non_neg_integer(),
     re_sent: non_neg_integer(),
     skipped: non_neg_integer()
   }}
  | {:error, term()}

Replays logged events to a CHOSEN endpoint — one event (:event_uuid) or a filtered set (:event_type with optional :from/:to) — including an endpoint that was disabled when the event happened or was added later. Replays KEEP the event id (a Standard Webhooks receiver deduplicates by it).

For each event: an endpoint with NO existing delivery row gets one (created, driven by your :enqueue seam or recovery), an endpoint with a TERMINAL row is re-armed through AshHooks.Delivery.resend/2 — fresh ladder, who asked recorded on the row — and a non-terminal row is left to its schedule (counted :skipped). Every replay records who asked: :requested_by (required) rides the row and the [:ash_hooks, :delivery, :resend] telemetry.

Options: :event_uuid or :event_type (+ :from/:to), :requested_by (required), :deliveries and :endpoints (required resource modules), :enqueue, :limit (max events this call; default 100, max 1,000), and :tenant. Returns {:ok, %{created: n, re_sent: n, skipped: n}}.