Chronicle.Transactions.UnitOfWork (cratis_chronicle v1.0.2)

Copy Markdown View Source

Buffers event appends and commits them as a single Chronicle append-many operation.

Use begin/1 to create a unit of work and make it current for the calling process. Calls to Chronicle.append/3, Chronicle.append_many/3, or a transactional event sequence are buffered until commit/1 is called.

Summary

Functions

Begins a new unit of work and makes it current for the calling process.

Returns a specification to start this module under a supervisor.

Commits the buffered events.

Returns the correlation id for a unit of work.

Returns the current unit of work for the calling process.

Returns the buffered events in insertion order.

Returns whether the calling process currently has an active unit of work.

Returns whether the unit of work has been committed or rolled back.

Returns whether the unit of work completed successfully.

Returns the last committed sequence number if available.

Registers a callback that runs when the unit of work is committed or rolled back.

Rolls back the unit of work and clears the buffered events.

Sets an existing unit of work as current for the calling process.

Tries to get a unit of work by correlation id.

Runs a function inside a unit of work and commits it if the function succeeds. Rolls the unit of work back if the function raises.

Types

commit_fun()

@type commit_fun() :: (map() -> {:ok, map()} | {:error, term()})

on_completed()

@type on_completed() :: (t() -> any())

t()

@type t() :: pid()

Functions

add_event(unit_of_work, event_sequence_id, event, opts \\ [])

Adds an event to the unit of work.

begin(opts \\ [])

@spec begin(keyword()) :: t()

Begins a new unit of work and makes it current for the calling process.

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

commit(unit_of_work)

@spec commit(t()) :: :ok | {:error, term()}

Commits the buffered events.

correlation_id(unit_of_work)

@spec correlation_id(t()) :: Chronicle.Correlation.CorrelationId.t()

Returns the correlation id for a unit of work.

current()

@spec current() :: t()

Returns the current unit of work for the calling process.

get_events(unit_of_work)

Returns the buffered events in insertion order.

has_current?()

@spec has_current?() :: boolean()

Returns whether the calling process currently has an active unit of work.

is_completed?(unit_of_work)

@spec is_completed?(t()) :: boolean()

Returns whether the unit of work has been committed or rolled back.

is_success?(unit_of_work)

@spec is_success?(t()) :: boolean()

Returns whether the unit of work completed successfully.

last_committed_sequence_number(unit_of_work)

@spec last_committed_sequence_number(t()) :: non_neg_integer() | nil

Returns the last committed sequence number if available.

on_completed(unit_of_work, callback)

@spec on_completed(t(), on_completed()) :: :ok

Registers a callback that runs when the unit of work is committed or rolled back.

rollback(unit_of_work)

@spec rollback(t()) :: :ok

Rolls back the unit of work and clears the buffered events.

set_current(unit_of_work)

@spec set_current(t()) :: t()

Sets an existing unit of work as current for the calling process.

try_get_for(correlation_id)

@spec try_get_for(Chronicle.Correlation.CorrelationId.t() | String.t()) ::
  {:ok, t()} | :error

Tries to get a unit of work by correlation id.

with_unit_of_work(fun, opts \\ [])

@spec with_unit_of_work(
  (t() -> result),
  keyword()
) :: result
when result: any()

Runs a function inside a unit of work and commits it if the function succeeds. Rolls the unit of work back if the function raises.