As I know, all operations in Akka.Net are asynchronous, and Context.Stop()
simply sends a Stop
message to the actor. That means, actor will be alive for some time before it completely shuts down.
And if I'll call Context.Child()
right after Context.Stop()
with the name of the actor I just stopped, I will get the same actor.
Here is the example code
var actor = context.Child(actorName);
if (actor.Equals(ActorRefs.Nobody))
{
actor = CreateNewActor();
}
Context.Stop(actor)
actor = context.Child(actorName);
// what do we get here, same actor or ActorRefs.Nobody ?
My application creates actors to process events from terminals. Each time new terminal connected, I create new actor by calling Context.Child()
using terminal name. When terminal disconnects, I stop the actor.
Problem is that some times I receive Connect message right after Disconnect for same terminal, and as result I get actor that's going be stopped. Is there any way to check that actor received Stop message and will be stopped soon?
You can use
var shutdown = actor.GracefulStop(TimeSpan.FromSeconds(42));
It returns a task whose result confirms shutdown within 42 seconds
UPDATE
But, in case, if you want to recreate actor later with the same name, you should listen to Terminated message within actor's supervisor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With