Accrue.Actor (accrue v0.3.0)

Copy Markdown View Source

Process-dictionary-backed actor context for the Events ledger (D-15).

Callers upstream of Accrue.Events.record/1 set the current actor via put_current/1 or scope a block with with_actor/2. Events.record/1 reads current/0 and stamps each event with the actor metadata.

The actor type is a fixed enum: :user | :system | :webhook | :oban | :admin. Unknown atoms raise ArgumentError at put time so typos fail loud.

Accrue.Plug.PutActor (Phase 2) and the Oban worker middleware wire this automatically; library callers can also manage it themselves with with_actor/2.

Summary

Functions

Reads the current actor from the process dictionary. Returns nil when unset.

Returns the current operation ID from the process dictionary, or nil if unset. Used by Accrue.Processor.Stripe as the seed for deterministic idempotency keys (D2-12).

Stores an actor in the process dictionary. Raises ArgumentError on unknown actor type.

Stores an operation ID in the process dictionary. Oban middleware and webhook plug set this automatically so downstream processor calls produce deterministic idempotency keys.

Returns the fixed actor-type enum. Useful for downstream validation.

Runs fun with actor as the current actor, restoring the prior value (or clearing it) in an after block.

Types

actor_type()

@type actor_type() :: :user | :system | :webhook | :oban | :admin

t()

@type t() :: %{type: actor_type(), id: String.t() | nil}

Functions

current()

@spec current() :: t() | nil

Reads the current actor from the process dictionary. Returns nil when unset.

current_operation_id()

@spec current_operation_id() :: String.t() | nil

Returns the current operation ID from the process dictionary, or nil if unset. Used by Accrue.Processor.Stripe as the seed for deterministic idempotency keys (D2-12).

current_operation_id!()

@spec current_operation_id!() :: String.t()

Raising variant of current_operation_id/0.

Behaviour depends on :idempotency_mode (D3-63):

put_current(actor)

@spec put_current(t() | nil) :: :ok

Stores an actor in the process dictionary. Raises ArgumentError on unknown actor type.

put_operation_id(id)

@spec put_operation_id(String.t() | nil) :: :ok

Stores an operation ID in the process dictionary. Oban middleware and webhook plug set this automatically so downstream processor calls produce deterministic idempotency keys.

types()

@spec types() :: [actor_type()]

Returns the fixed actor-type enum. Useful for downstream validation.

with_actor(actor, fun)

@spec with_actor(t(), (-> any())) :: any()

Runs fun with actor as the current actor, restoring the prior value (or clearing it) in an after block.