public
Created

Worker of the Master / Worker remote node pattern

  • Download Gist
Worker.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
abstract class Worker(masterLocation: ActorPath)
extends Actor with ActorLogging {
import MasterWorkerProtocol._
 
// We need to know where the master is
val master = context.actorFor(masterLocation)
 
// This is how our derivations will interact with us. It
// allows dervations to complete work asynchronously
case class WorkComplete(result: Any)
 
// Required to be implemented
def doWork(workSender: ActorRef, work: Any): Unit
 
// Notify the Master that we're alive
override def preStart() = master ! WorkerCreated(self)
 
// This is the state we're in when we're working on something.
// In this state we can deal with messages in a much more
// reasonable manner
def working(work: Any): Receive = {
// Pass... we're already working
case WorkIsReady =>
// Pass... we're already working
case NoWorkToBeDone =>
// Pass... we shouldn't even get this
case WorkToBeDone(_) =>
log.error("Yikes. Master told me to do work, while I'm working.")
// Our derivation has completed its task
case WorkComplete(result) =>
log.info("Work is complete. Result {}.", result)
master ! WorkIsDone(self)
master ! WorkerRequestsWork(self)
// We're idle now
context.become(idle)
}
 
// In this state we have no work to do. There really are only
// two messages that make sense while we're in this state, and
// we deal with them specially here
def idle: Receive = {
// Master says there's work to be done, let's ask for it
case WorkIsReady =>
log.info("Requesting work")
master ! WorkerRequestsWork(self)
// Send the work off to the implementation
case WorkToBeDone(work) =>
log.info("Got work {}", work)
doWork(sender, work)
context.become(working(work))
// We asked for it, but either someone else got it first, or
// there's literally no work to be done
case NoWorkToBeDone =>
}
 
def receive = idle
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.