Graceful shutdown on typed persistent actor

Hi, I’m using both ClusterSharding and EventSourcedBehavior.

I think EventSourcedBehavior supports graceful shutdown by waiting persisting & processing stashed commands before stop. Right?

ClusterSharding also send PoisonPill(by default) to Entity on rebalancing entities.

The unfortunate thing is we don’t use EventSourcedBehavior under ClusterSharding directly.
We shard actor X, and create persistent actor Y under X.

What I want:

  • Cluster shard sends stop message to X (I know it’s possible since stop message can be configured)
  • X sends stop message to Y (Difficult part: because I cannot send PoisonPill)
  • X turns into waiting state
  • When Y stops, X also stops.

How can I implement it?

That shouldn’t be a problem!

First you’ll need a stop command as part of the messages Y accepts, let’s call that StopY, that makes it return the stopped effect. You’ll also need a graceful stop command for X, let’s call that StopX.

  • Configure sharding to use StopX for stopping entitles.
  • When you get the stop message to X you watch Y and then send it StopY
  • In X´ you now use the stop` effect.
  • Since you watched Y from X you will get a Terminated message to X when Y has stopped and can stop X as well.