Comments (2)
I've done this on my own. It is not hard, just tricky.
Yes, you need to callbacks on the aggregate:
save_to_memento
load_from_memento(memento)
These would in the same place as the apply
method is right now.
For snapshot stream, I use AggName-snapshots-AggID
. So for aggregate Foo
with id 123
it would be Food-snapshots-123
.
Loading snapshots
The process that does "loading" from snapshots and events I call the hydrator, and here is there relevant piece of code (I'm sure your implementation is much more elegant, but you get the gist):
def do_hydrate_from_snapshot(%Hydrator{snapshot_stream_id: id}=state) do
state2 = case EvStore.read_events_backward(id, -1, 1) do
{:error, :NoStream, _} -> state
{:ok, %{events: events}} ->
[snapshot] = Adaptor.from_external(events)
hydrate_from_snapshot(snapshot, state)
end
# Now start hydrating from events
send self(), :hydrate_from_events
{:noreply, state2}
end
def hydrate_from_snapshot(%Snapshot{memento: m, event_number: n}, %Hydrator{agg_mod: agg_mod, agg_pid: agg_pid}=state) do
:ok = agg_mod.load_from_memento(agg_pid, m)
%{state | last_event: n, last_snapshot_event: n}
end
Basically, you want to get the "last" event. If there is none, e.g. no snapshot available, just continue hydrating from events. If there is a snapshot, deserialize it and give it to aggregate.
Saving snapshots
For saving the snapshots, I set a periodic timer:
:timer.send_after(@save_period, :save)
and then check if it is time to save the snapshot (in this case @snapshot_period 1000
- so snapshot about every thousand events - this works out very well, for Gregg's ES because 5K is when it starts to get slow):
def save_snapshot(%{agg_pid: agg_pid,
agg_mod: agg_mod,
last_event: last_event,
snapshot_stream_id: snapshot_stream_id,
last_snapshot_event: last_snapshot_event}=state) do
case (last_event - last_snapshot_event) > @snapshot_period do
true ->
{:ok, memento} = agg_mod.save_to_memento(agg_pid)
event = %Snapshot{event_number: last_event, memento: memento}
external = Adaptor.to_external([event])
{:ok, _} = EvStore.append_events(snapshot_stream_id, external, -2)
%{state | last_snapshot_event: last_event}
false ->
state
end
end
from commanded.
@drozzy Thanks for the feedback.
from commanded.
Related Issues (20)
- General design philosophy question: sending email from process manager HOT 4
- update FAQ regarding scheduling HOT 1
- Wiki Testing section is outdated HOT 1
- warning: redefining module Commanded.Serialization.JsonDecoder.Any HOT 2
- Event number gaplessness required?
- Commanded.aggregate_state does not work when aggregate identity has a prefix HOT 10
- Process manager router option not working
- Lessons learned from performance optimization - an unlikely culprit HOT 3
- no function clause matching in Commanded.Commands.Dispatcher.telemetry_stop/3 HOT 1
- Docs questions
- Stacktrace in event handler error? HOT 2
- Paralelization Strategies in EventHandlers
- Should Commanded.Event.Handler support messages from swarm? HOT 2
- Event retention policies?
- please support multiple commanded application with one eventstore HOT 6
- Process Manager state serialization breaks when using a custom TypeProvider with the JsonSerializer
- `Commanded.ProcessManagers.ProcessManager.identity/0` function returns `nil` in unit tests
- no function clause matching in Commanded.Event.Handler.partition_event/4 HOT 1
- EventstoreDB is sunsetting the TCP protocol HOT 1
- Is it a bad practice for an event handler to depend on a projector completion? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commanded.