Restarting Akka Actor with a new State

38 views
Skip to first unread message

Joe San

unread,
Aug 7, 2017, 3:28:11 PM8/7/17
to Akka User List

I have a scenario where I have to restart a child Actor from within a parent actor. The restart should happen with the following rules:

  1. The start should happen only after stop has been completed
  2. Both the stop and start should happen asynchronously

I now have the following scenario:


In my parent Actor, I have a Monix Observable which is pushing events as below:


class ParentActor extends Actor {
  ...

    override def preStart(): Unit = {
    super.preStart()

    // Observable to stream events regarding PowerPlant's
    val powerPlantEventObservable =
    // For every config.database.refreshInterval in seconds
      Observable.interval(config.database.refreshInterval)
        // We ask the actor for the latest messages
        .map(_ => (dbServiceActor ? DBServiceActor.PowerPlantEvents).mapTo[PowerPlantEventsSeq])
        .concatMap(Observable.fromFuture(_))
        .concatMap(Observable.fromIterable(_))

    // Subscriber that pipes the messages to this Actor
    cancelable := powerPlantEventObservable.subscribe { update =>
      (self ? update).map(_ => Continue)
    }
  }
}


So what happens above is that, I'm asking another Actor called DBServiceActor for a List of events and when these events are available, I'm piping it to the ParentActor (self ? update). The receive method of the ParentActor looks like this and this is where I want to restart my child actor . asynchronously:


    override def receive: Receive = {

        case PowerPlantUpdateEvent(id, powerPlantCfg) =>
          log.info(s"Re-starting PowerPlant actor with id = $id and type ${powerPlantCfg.powerPlantType}")

          // I want to stop the actor first by finding it from the actor system
          // If it exists, do a context.stop on the Actor instance
          // Once it is stopped, I want to start it again by creating a new instance of this Actor
          // Once this new Actor instance is created, I want to signal my Monix Observer to send me the next event
      }


Any suggestions?

Akka Team

unread,
Aug 11, 2017, 4:00:58 AM8/11/17
to Akka User List
Hi Joe,

Watching the child actor before stopping it will give you a Terminated message which will let you know that the child terminated, so that you can start a new one. If the id and powerPlantCfg is needed when starting anew you can put that in the actor state, as a map for keyed by the child ActorRef for example, so that you can look it up when the Terminated message arrives.

--
Johan
Akka Team

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages