TCC.Action (toccata v0.1.0)

Copy Markdown View Source

Represents a single TCC action in a distributed transaction.

Each action consists of three phases:

  • Try: Reserve resources and validate
  • Confirm: Commit the changes
  • Cancel: Rollback and release resources

Summary

Types

Callback type - can be an anonymous function or MFA tuple.

t()

Functions

Returns true if the action is async.

Executes the Cancel phase of the action with retry logic.

Executes the Confirm phase of the action with retry logic.

Executes the Try phase of the action.

Types

callback()

@type callback() :: function() | {module(), atom(), list()}

Callback type - can be an anonymous function or MFA tuple.

t()

@type t() :: %TCC.Action{
  async: boolean(),
  cancel_fun: callback(),
  confirm_fun: callback(),
  name: atom(),
  opts: map(),
  try_fun: callback()
}

Functions

async?(action)

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

Returns true if the action is async.

execute_cancel(action, effects, params)

@spec execute_cancel(t(), map(), any()) :: {:ok, map(), any()} | {:error, any()}

Executes the Cancel phase of the action with retry logic.

execute_confirm(action, effects, params)

@spec execute_confirm(t(), map(), any()) :: {:ok, map(), any()} | {:error, any()}

Executes the Confirm phase of the action with retry logic.

execute_try(action, effects, params)

@spec execute_try(t(), map(), any()) ::
  {:ok, map(), any()} | {:error, any()} | {:abort, any()}

Executes the Try phase of the action.

The try function can return:

  • {:ok, effects, params} - Success, continue to next action
  • {:error, reason} - Error, trigger cancel phase (allows retry)
  • {:abort, reason} - Unrecoverable error, trigger cancel phase (no retry)

new(name, try_fun, confirm_fun, cancel_fun, opts \\ [])

@spec new(atom(), callback(), callback(), callback(), keyword()) :: t()

Creates a new TCC action.

Callbacks can be either anonymous functions or MFA tuples {Module, :function, args}. When using MFA tuples, effects and params are prepended to the args list.

Examples

# Using anonymous functions
iex> TCC.Action.new(:payment, &try_fn/2, &confirm_fn/2, &cancel_fn/2)
%TCC.Action{name: :payment, ...}

# Using MFA tuples
iex> TCC.Action.new(:payment,
...>   {PaymentService, :try_payment, []},
...>   {PaymentService, :confirm_payment, []},
...>   {PaymentService, :cancel_payment, []})
%TCC.Action{name: :payment, ...}

new_async(name, try_fun, confirm_fun, cancel_fun, opts \\ [])

@spec new_async(atom(), callback(), callback(), callback(), keyword()) :: t()

Creates a new async TCC action.

Async actions are executed in parallel during the Try phase. They are awaited before the next synchronous action or at the end of the Try phase.

Options

  • :timeout - Time in milliseconds to wait for the async action (default: 5000)

Example

TCC.new()
|> TCC.run_async(:payment, &try_fn/2, &confirm_fn/2, &cancel_fn/2, timeout: 10_000)