Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to wait for actor to be completely stopped?

Tags:

c#

akka

akka.net

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?

like image 628
bonzaster Avatar asked Oct 30 '22 14:10

bonzaster


1 Answers

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.

like image 150
Yury Glushkov Avatar answered Nov 15 '22 04:11

Yury Glushkov