work pulling with akka persistence for Master's work queue state

66 views
Skip to first unread message

Greg Flanagan

unread,
Aug 12, 2014, 6:46:01 PM8/12/14
to akka...@googlegroups.com
I'm using the work pulling pattern and wanted to add persistence to the Master's work Queue state via akka persistence. I've come up with an approach and wanted to get feedback.

I'm using a muttable.Queue to store the current work and so I need to persist Enqueue and Dequeue events. The Enqueue event is straightforward, just add the work to the queue. However the Dequeue event feels a bit strange in my approach since I need to return the value of calling dequeue. To get around the fact that the function passed to persist has to return Unit I'm storing the the return value of dequeue in a var. Here is a code snippet:

class class Master[T] extends PersistentActor {

  val persistenceId = "workpoolMaster"
 
  private val workers = Map.empty[ActorRef, Option[(ActorRef, T)]]

  private val queue = Queue.empty[T]

  private var work: Option[T] = None
 
  def enqueue(enqueue: Enqueue[T]) = queue += enqueue.work
 
  def dequeue(ignore: Dequeue) = {
    if (!queue.isEmpty) work = Some(queue.dequeue)
    else work = None
  }

  val receiveRecover: Receive = {
    case work: Enqueue[T] => enqueue(work)
    case Dequeue => dequeue(Dequeue)
  }

  val receiveCommand: Receive = {

    case work: Work[T] => {
      persist(Enqueue(work.work))(enqueue)
      notifyWorkers()
    }

    case GimmeWork => {
      persist(Dequeue)(dequeue)
      work foreach { w =>
        workers += (sender -> Some(sender -> w))
        sender ! Work(w)
      }
    }

...




Does this look like a reasonable approach?

Cheers,
Greg


Will Sargent

unread,
Aug 13, 2014, 1:37:39 AM8/13/14
to akka...@googlegroups.com
You should look at 


which uses akka persistence with a work-pulling master.

Will Sargent
Consultant, Professional Services
Typesafe, the company behind Play Framework, Akka and Scala


--
>>>>>>>>>> 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+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Greg Flanagan

unread,
Aug 13, 2014, 11:04:42 AM8/13/14
to akka...@googlegroups.com
looks really good. thanks for passing along.
Reply all
Reply to author
Forward
0 new messages