I'm trying to build a messaging process between akka actors to represent the master giving the worker a job, and keeping a close eye on it. My question is
- Is what I'm proposing below a reasonable approach, and
- even if it's not, I'd like to know how it could be done properly with composition of Futures, for the sake of my Future education.
The process I'd like goes something like this
1) Master sends work to Worker with an ask
. It expects a reply within 5 seconds, else it considers the worker to have lost it's chance and it will have to enter bidding again.
import context.dispatcher
implicit val timeout = Timeout(5 seconds)
val workCompletedFuture = (worker ? WorkTicket(work)).mapTo[Future[WorkCompleted]]
2a) If the worker doesn't respond within the 5 seconds I'd like the master to send itself a message saying to reallocate the work.
self ! WorkAllocationFailed(work, worker)
2b) If the worker did respond then it gives us a Future[WorkCompleted]. I'd like to wait for that future to be completed for up to, say, 2 minutes.
3a) If Future[WorkCompleted] fails to complete within the timeout then reallocate the work
self ! WorkFailed(work, worker)
3b) If the Future[WorkCompleted] succeeds then collect the result
I've tried creating this logic, but I get in a mess with nested onComplete
, and I don't know how to do the timeout on the Future[WorkCompleted]. I tried reading the Akka 2.10 Futures docs, but couldn't figure out a solution.