Craftsman at Work

I'm Artur Karbone, coding software architect and independent IT consultant and this is my blog about craftsmanship, architecture, distributed systems, management and much more.

Akka.NET: Restart an Actor programmatically

I've seen a couple of questions on the Internet regarding how to restart an actor programmatically.
To my knowledge, there is no way to send some analogue of PoisonPill.Instance to restart an actor. However, it is doable programmatically.

First of all, you need a message. I assume it's going to be a marker message with no properties. Something like this:

class RestartMessage  
    {

    }

Now you need to handle the message. Cast Self to IInternalActorRef and call Restart method:

 Receive<RestartMessage>(x => ((IInternalActorRef)Self).Restart(new Exception("Some Reason")));

I also tried to play with Start/Stop implementations of IInternalActorRef. But it didn't work out (the messages are going to be send to dead letter queue after stopping the actor). Have to dig into Restart internals to figure out how it works and why obvious Stop/Start doesn't.

  Receive<RestartMessageDoNotUseThisApproach>(x =>
            {
                //Do not use this approach. All messages received after stop will be lost?
                var internalRef = ((IInternalActorRef)Self);
                internalRef.Stop();
                internalRef.Start();
            });

Here is a link to GitHub Repository if you want to play with the whole sample.

comments powered by Disqus