Commanded v0.14.0-rc.0 Commanded.ProcessManagers.ProcessManager behaviour View Source
Behaviour to define a process manager.
A process manager is responsible for coordinating one or more aggregate roots. It handles events and dispatches commands in response. Process managers have state that can be used to track which aggregate roots are being orchestrated.
Use the Commanded.ProcessManagers.ProcessManager
macro in your process
manager module and implement the three callback functions defined in the
behaviour:
Example
defmodule ExampleProcessManager do
use Commanded.ProcessManagers.ProcessManager,
name: "ExampleProcessManager",
router: BankRouter
def interested?(%AnEvent{...}) do
# ...
end
def handle(%ExampleProcessManager{...}, %AnEvent{...}) do
# ...
end
def apply(%ExampleProcessManager{...}, %AnEvent{...}) do
# ...
end
end
Start the process manager (or configure as a worker inside a Supervisor)
{:ok, process_manager} = ExampleProcessManager.start_link()
Consistency
For each process manager you can define its consistency, as one of either
:strong
or :eventual
.
This setting is used when dispatching commands and specifying the consistency
option.
When you dispatch a command using :strong
consistency, after successful
command dispatch the process will block until all process managers configured to
use :strong
consistency have processed the domain events created by the
command.
The default setting is :eventual
consistency. Command dispatch will return
immediately upon confirmation of event persistence, not waiting for any
process managers.
Example
defmodule ExampleProcessManager do
use Commanded.ProcessManagers.ProcessManager,
name: "ExampleProcessManager",
router: BankRouter
consistency: :strong
# ...
end
Please read the Process managers guide for more details.
Link to this section Summary
Callbacks
Mutate the process manager’s state by applying the domain event
Process manager instance handles the domain event, returning commands to dispatch
Is the process manager interested in the given command?
Link to this section Types
Link to this section Callbacks
apply(process_manager, domain_event) :: process_manager
Mutate the process manager’s state by applying the domain event
The apply/2
function is used to mutate the process manager’s state.
It receives its current state and the interested event.
It must return the modified state.
handle(process_manager, domain_event) :: [command]
Process manager instance handles the domain event, returning commands to dispatch
A handle/2
function must exist for each :start
and :continue
tagged event previously specified.
It receives the process manager’s state and the event to be handled.
It must return the commands to be dispatched. This may be none, a single command, or many commands.
interested?(domain_event) :: {:start, process_uuid} | {:continue, process_uuid} | {:stop, process_uuid} | false
Is the process manager interested in the given command?
The interested?/1
function is used to indicate which events the process manager receives.
The response is used to route the event to an existing instance or start a new process instance.
- Return
{:start, process_uuid}
to create a new instance of the process manager. - Return
{:continue, process_uuid}
to continue execution of an existing process manager. - Return
{:stop, process_uuid}
to stop an existing process manager and shutdown its process. - Return
false
to ignore the event.