I have an actor system with persistent actor A. On receiving message M, this should spawn an instance of persistent actor B, that executes some dangerous and long-running process (involving exchanging other messages with other parties), and sends back message N to A. Upon receiving N, A should terminate B.
Spawining is implemented this way: when A receives M, it validates, calculates and creates an event M', that is persisted. When the event is applied, A spawns the child with the precalculated information. If the system is restarted at this point, M' will be replayed to A, and it will create a new incarnation of the same child B.
What I am struggling with is handling the case of terminated children when recovering the system: I would like to not see any Bs that were terminated before restarting the system.
Initially I just sent poison pill messages from the parent, but since the persistent actors do not store any event about receiving of such a command, and just die kindly, when the system is recovering, this last chapter of their story is not replayed to them, and they just keep hanging around.
I took a different approach and tried to call Context.Stop(child)
when processing the recovery messages of A, but that lead to all these Bs being terminated before they could recover, causing the system to log issues, like recovery timing out.
So I guess I either have to let the B recover before being killed, or not recreate it in the first place.
What I am now trying to do is to introduce a flag in the object representing the state of B, creating a custom message to be sent instead of PoisonPill, so that B can persist an event of being terminated, and when it receives the information of the recovery being completed, checking this field and terminating itself. But it looks like hell of a lot of work for a simple requirement of not resurrecting dead actors upon restart, so I am wondering if I am doing something completely wrong or trying to reinvent hot water.