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
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
Returns the module for a saga.
Specs
resume(Cizen.SagaID.t(), t(), state(), [start_option()]) :: GenServer.on_start()
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.
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
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.
Specs
The handler for Saga.cast/2
.
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
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
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.