Commanded v0.16.0 Commanded.Aggregates.Aggregate View Source
Aggregate is a GenServer
process used to provide access to an
instance of an event sourced aggregate.
It allows execution of commands against an aggregate instance, and handles persistence of created events to the configured event store. Concurrent commands sent to an aggregate instance are serialized and executed in the order received.
The Commanded.Commands.Router
module will locate, or start, an aggregate
instance when a command is dispatched. By default, an aggregate process will
run indefinitely once started. Its lifespan may be controlled by using the
Commanded.Aggregates.AggregateLifespan
behaviour.
Snapshotting
You can configure state snapshots for an aggregate in config. By default snapshots are not taken for an aggregate. The following options are available to enable snapshots:
snapshot_every
- snapshot aggregate state every so many events. Usenil
to disable snapshotting, or exclude the configuration entirely.snapshot_version
- a non-negative integer indicating the version of the aggregate state snapshot. Incrementing this version forces any earlier recorded snapshots to be ignored when rebuilding aggregate state.
Example
In config/config.exs
enable snapshots for ExampleAggregate
after every ten
events:
config :commanded, ExampleAggregate
snapshot_every: 10,
snapshot_version: 1
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor
Execute the given command against the aggregate
Invoked when the server is started. start_link/3
or start/3
will
block until it returns
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
Execute the given command against the aggregate.
aggregate_module
- the aggregate’s module (e.g.BankAccount
).aggregate_uuid
- uniquely identifies an instance of the aggregate.context
- includes command execution arguments (seeCommanded.Aggregates.ExecutionContext
for details).timeout
- an integer greater than zero which specifies how many milliseconds to wait for a reply, or the atom :infinity to wait indefinitely. The default value is five seconds (5,000ms).
Return values
Returns {:ok, aggregate_version, events}
on success, or {:error, reason}
on failure.
aggregate_version
- the updated version of the aggregate after executing the command.events
- events produced by the command, can be an empty list.
Invoked when the server is started. start_link/3
or start/3
will
block until it returns.
args
is the argument term (second argument) passed to start_link/3
.
Returning {:ok, state}
will cause start_link/3
to return
{:ok, pid}
and the process to enter its loop.
Returning {:ok, state, timeout}
is similar to {:ok, state}
except handle_info(:timeout, state)
will be called after timeout
milliseconds if no messages are received within the timeout.
Returning {:ok, state, :hibernate}
is similar to
{:ok, state}
except the process is hibernated before entering the loop. See
c:handle_call/3
for more information on hibernation.
Returning :ignore
will cause start_link/3
to return :ignore
and the
process will exit normally without entering the loop or calling c:terminate/2
.
If used when part of a supervision tree the parent supervisor will not fail
to start nor immediately try to restart the GenServer
. The remainder of the
supervision tree will be (re)started and so the GenServer
should not be
required by other processes. It can be started later with
Supervisor.restart_child/2
as the child specification is saved in the parent
supervisor. The main use cases for this are:
- The
GenServer
is disabled by configuration but might be enabled later. - An error occurred and it will be handled by a different mechanism than the
Supervisor
. Likely this approach involves callingSupervisor.restart_child/2
after a delay to attempt a restart.
Returning {:stop, reason}
will cause start_link/3
to return
{:error, reason}
and the process to exit with reason reason
without
entering the loop or calling c:terminate/2
.
Callback implementation for GenServer.init/1
.