kenlz
kenlz

Reputation: 461

Find Actor Status Akka Scala

Currently I have rest-endpoint-api-playframework that contains main actor to run n number of worker-actors doing batch-insertion. while in the process of doing that, I'd love to get current process of the inserts, lets say 100/1000. in order to do that, obviously I'll have the actor to report to the sender what is the status of the process.

class Application (implicit inj : Injector) extends Controller with Injectable {
  implicit val timeout = Timeout(5 seconds)
  val mainActor = system.actorOf(RoundRobinPool(100).props(Props(new SupervisorActor(0))), name = "helloactor")

  val future = mainActor ? ProcessBatch  

  val workerFuture = mainActor ? CurrentProcessedItem
  result = Await.result(workerFuture, timeout.duration).asInstanceOf[String] 

  Ok(Json.obj("return"->result.toString))  
}

above, I'd expect that if helloActor ? ProcessBatch has completed, then I'd just return the process is completed, otherwise I'd run the mainActor ? CurrentProcessedItem that returns a future of the currentProcessedItem.

So basically I will need an indicator whether mainActor ? ProcessBatch has completed or not. is it possible?

Upvotes: 0

Views: 137

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14816

"Ask" returns you are Future. Therefore, onComplete you can do whatever you want:

val future = helloActor ? ProcessBatch
future.onComplete {
    case Success(result) => // Do stuff
}

However, I'd rather run helloActor ? ProcessBatch within mainActor and "pipe" the result to mainActor.

Upvotes: 0

Related Questions