In my app, I need an actor to get some information from another actor either at, or immediately after startup. This information needs to be used to return another behavior, since that actor cannot do much before this happens. The problem that I am seeing is that it has to obtain the information from another actor, so that requires a future. So far, this is what I have, but I would like to know if this is a/the right way to go about something like this:
def apply(iterationNumber: Int, localPlayerId: String, maxIterationSeconds: Int)
(implicit timeout: Timeout, scheduler: Scheduler, ec: ExecutionContext): Behavior[IterationCommand] = {
Behaviors.setup { context =>
val receptionist = context.system.receptionist
context.system.receptionist ! Receptionist.Register(iterationServiceKey, context.self)
val temporaryBehavior = Behaviors.receiveMessage[IterationCommand] {
case ElectedLeaderId(leaderId) =>
val timer = context.scheduleOnce((3 * maxIterationSeconds).seconds, context.self, StopIteration)
iterationBehavior(iterationNumber, localPlayerId, leaderId, Seq.empty, receptionist, timer, context)
case _ =>
Behaviors.unhandled
}
electLeader(iterationNumber, receptionist, context.self)
temporaryBehavior
}
}
the electLeader
method calls out to another actor to get some information so that it can do that election.