Replies: 1 comment 1 reply
-
What you can do is use behavior switching to switch to a stashing behavior when you're saving your snapshot and then switch back to normal message processing when you receive these signals. Here's a simple example on how to do this, recovery code is ommited for brevity: public sealed class MyPersistedCommand { }
public class SnapshotPersistentActor : ReceivePersistentActor
{
public SnapshotPersistentActor(string persistenceId)
{
PersistenceId = persistenceId;
Become(Active);
}
public override string PersistenceId { get; }
private void Active()
{
Command<MyPersistedCommand>(cmd =>
{
SaveSnapshot(cmd);
Become(Saving);
});
}
private void Saving()
{
Command<SaveSnapshotSuccess>(_ =>
{
Stash.UnstashAll();
Become(Active);
});
Command<SaveSnapshotFailure>(fail =>
{
// Process failure here
Stash.UnstashAll();
Become(Active);
});
CommandAny(_ => Stash.Stash());
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've had a few use cases where I need an actor's state to be persisted to some external store, but implementing full EventSourcing would be over-architecting the project. The actors may receive a message a week at a time, or multiple messages at once. I'm using SnapshotStore as a temporary solution, but using the SnapshotStore in this way seems wrong to me. Some of the features that come with Akka.Persistence are directly tied to fully implementing Events and maybe they shouldn't be.
The main example is Persisting Events vs Persisting Snapshots. Each persistent actor has an internal
EventsourcedState
that helps protect from executing the next commands before saving to the db is complete. My actor will not execute its next command until the event produced by the previous command is persisted. The same protections are not given to Snapshots, which is expected if an actor is built upon Events. It would be nice if I have that protection for an actor that is built only by Snapshots. Using the Defer() method also falls in the same line, where I want to respond to a successful save when it happens, but not out of receiving message order.The alternative implementation is I could just handle the state switching and recovery myself without using Akka.Persistence, but I know I'll miss some feature or performance improvement that comes inherently with Akka.Persistence.
I'm not proposing updating or modifying Akka.Persistence, it's a great module. I'm more curious if there should be a fork (maybe with a different name besides Akka.Persistence) that would include the executing command protections for a Persistent state. There's probably other features that are given to Events and not Snapshots that I'm missing too. The currently supported db packages would probably still work with it too, it's mainly the internals of Akka that could use some tweaking.
Alright, rambling done, I'm curious what the community thinks.
Beta Was this translation helpful? Give feedback.
All reactions