Cizen.Saga behaviour (Cizen v0.18.1) View Source

The saga behaviour

Example

defmodule SomeSaga do
  use Cizen.Saga
  defstruct []

  @impl true
  def on_start(%__MODULE__{}) do
    saga
  end

  @impl true
  def handle_event(_event, state) do
    state
  end
end

Link to this section Summary

Functions

Returns the pid for the given saga ID.

Returns the saga struct for the given saga ID.

Returns the module for a saga.

Resumes a saga with the given state.

Starts a saga.

Starts a saga linked to the current process.

Callbacks

The handler for Saga.call/2.

The handler for Saga.cast/2.

Invoked when the saga receives an event.

Invoked when the saga is resumed.

Invoked when the saga is started. Saga.Started event will be dispatched after this callback.

Link to this section Types

Specs

lifetime() :: pid() | {atom(), node()} | atom() | nil

Specs

start_option() ::
  {:saga_id, Cizen.SagaID.t()}
  | {:lifetime, pid() | Cizen.SagaID.t() | nil}
  | {:return, :pid | :saga_id}
  | {:resume, term()}

Specs

state() :: any()

Specs

t() :: struct()

Link to this section Functions

Specs

get_pid(Cizen.SagaID.t()) :: {:ok, pid()} | :error

Returns the pid for the given saga ID.

Specs

get_saga(Cizen.SagaID.t()) :: {:ok, t()} | :error

Returns the saga struct for the given saga ID.

Specs

module(t()) :: module()

Returns the module for a saga.

Link to this function

resume(id, saga, state, opts \\ [])

View Source

Specs

Resumes a saga with the given state.

Options:

  • {:lifetime, lifetime_saga_or_pid} the lifetime saga ID or pid. (Default: the saga lives forever)
  • {:return, return_type} when :saga_id, {:ok, saga_id} is returned. (Default: :pid)

Specs

start(t(), opts :: [start_option()]) :: GenServer.on_start()

Starts a saga.

Options:

  • {:saga_id, saga_id} starts with the specified saga ID. (Default: randomly generated)
  • {:lifetime, lifetime_saga_or_pid} the lifetime saga ID or pid. (Default: the saga lives forever)
  • {:return, return_type} when :saga_id, {:ok, saga_id} is returned. (Default: :pid)
  • {:resume, state} when given, resumes the saga with the specified state.
Link to this function

start_link(saga, opts \\ [])

View Source

Specs

start_link(t(), opts :: [start_option()]) :: GenServer.on_start()

Starts a saga linked to the current process.

See Saga.start/2 for details.

Specs

stop(Cizen.SagaID.t()) :: :ok

Link to this section Callbacks

Link to this callback

handle_call(message, from, state)

View Source

Specs

handle_call(message :: term(), from :: GenServer.from(), state()) :: state()

The handler for Saga.call/2.

You should call Saga.reply/2 with from, otherwise the call will be timeout. You can reply from any process, at any time.

Link to this callback

handle_cast(message, state)

View Source

Specs

handle_cast(message :: term(), state()) :: state()

The handler for Saga.cast/2.

Link to this callback

handle_event(arg1, state)

View Source

Specs

handle_event(Cizen.Event.t(), state()) :: state()

Invoked when the saga receives an event.

Returned value will be used as the next state to pass handle_event/2 callback.

Specs

on_resume(t(), state()) :: state()

Invoked when the saga is resumed.

Returned value will be used as the next state to pass handle_event/2 callback.

This callback is predefined. The default implementation is here:

def on_resume(saga, state) do
  on_start(saga)
  state
end

Specs

on_start(t()) :: state()

Invoked when the saga is started. Saga.Started event will be dispatched after this callback.

Returned value will be used as the next state to pass handle_event/2 callback.